# Ignoring derives in unused fields lints

**URL:** <https://internals.rust-lang.org/t/ignoring-derives-in-unused-fields-lints/15347>\
**Category:** language design\
**Created:** [September 18, 2021, 10:28am UTC](https://internals.rust-lang.org/t/ignoring-derives-in-unused-fields-lints/15347 "2021-09-18T10:28:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kixunil](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kixunil/32/4110_2.png) [@Kixunil](https://internals.rust-lang.org/u/Kixunil)\
**Post date:** [September 18, 2021, 10:28am UTC](https://internals.rust-lang.org/t/ignoring-derives-in-unused-fields-lints/15347/1 "2021-09-18T10:28:48Z")

</div>

Would it be possible to optionally ignore uses of fields in derive macros when computing unused lints?

Real world scenario:

I have a few thousands lines of code with multiple structs that have `#[derive(Debug)]` slapped on them. Sometimes I need to add a new field, so I go ahead and add it. I get missing field errors in constructors (or not if they are only constructed via `#[derive(Deserialize)]`!) but if I forget to also read those fields (which actually happened to me a few times) I have a bug that the compiler doesn't warn me about although it could.

Minimal example:

```rust
#[derive(Debug)]
struct Foo {
    a: &'static str,
}

fn main() {
    let foo = Foo {
        a: "hello",
    };
    
    println!("{:?}", foo);
}

```

[playground - see that the compiler doesn't warn about this](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=02f3f1506471ee320fe653b66599469c)

Edit: forgot to say that I'm also writing another crate where forgetting to read a field could cause vulnerabilities.

I wonder how hard would it be to get some crate-level attribute to turn this into a warning. Something like `warn(field_only_read_in_derived_code)` and probably also `warn(field_only_read_in_derived_code(Debug))` to turn this on only for certain derives, `Debug` probably being the most common in practice.

Did something similar happen to you? Do you have a better idea what to do about it? I'd love to know!

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [September 18, 2021, 11:31am UTC](https://internals.rust-lang.org/t/ignoring-derives-in-unused-fields-lints/15347/2 "2021-09-18T11:31:19Z")

</div>

Already implemented in nightly! It did turned up a bunch of warning all other the place, see [Unactionable "field is never read" warning for printed structs with the Debug derive · Issue #88900 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/88900)

---

<div class="post-metadata">

**Author:** ![Kixunil](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kixunil/32/4110_2.png) [@Kixunil](https://internals.rust-lang.org/u/Kixunil)\
**Post date:** [October 13, 2021, 5:17pm UTC](https://internals.rust-lang.org/t/ignoring-derives-in-unused-fields-lints/15347/3 "2021-10-13T17:17:53Z")

</div>

Awesome, thanks for pointers! (IDK why I only noticed your comment now.)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [January 11, 2022, 5:17pm UTC](https://internals.rust-lang.org/t/ignoring-derives-in-unused-fields-lints/15347/4 "2022-01-11T17:17:57Z")

</div>

Closed on January 11, 2022, 5:17pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
