Skip to content

derive(PartialEq) should not prevent "field is never read" warnings #134588

Open
@Wilfred

Description

@Wilfred

Code

#[derive(PartialEq)]
struct MyStruct {
    x: i32,
    y: i32, // no unused field warning, unfortunately
}

struct MyStruct2 {
    x: i32,
    y: i32, // warning today
}

pub fn use_struct() {
    let ms = MyStruct { x: 1, y: 2 };
    let _ = ms.x;

    let ms = MyStruct2 { x: 1, y: 2 };
    let _ = ms.x;
}

Current output

warning: field `y` is never read
 --> src/lib.rs:9:5
  |
7 | struct MyStruct2 {
  |        --------- field in this struct
8 |     x: i32,
9 |     y: i32, // warning today
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

Desired output

A warning for both MyStruct and MyStruct2.

Rationale and extra context

This was originally discussed on #84647 and #85200, although the conclusion there was to only exclude Debug and Clone.

However, it's really common to derive PartialEq on a type, especially when writing tests.

This means that adding tests can subtly stop this warning from catching issues. As far as I can see, there isn't a way to opt-in to stricter behaviour with either rustc or clippy here. There's no equivalent of must_use for struct fields, for example.

This issue was the root of a nasty bug for me. Would you be open to making this diagnostic fire for more automatically derived traits?

Other cases

Rust Version

Reproduced on rustc 1.83 on the playground.

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions