Skip to content

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

@Wilfred

Description

@Wilfred
Contributor

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Dec 20, 2024
changed the title [-]derive(PartialEq) shosuld not prevent "field is never read" warnings[/-] [+]derive(PartialEq) should not prevent "field is never read" warnings[/+] on Dec 20, 2024
ionicmc-rs

ionicmc-rs commented on Dec 21, 2024

@ionicmc-rs
Contributor

Ok ok i reproduced this and it seems only impls of Debug, Clone, and Default are ignored
look at this code:

#[derive(PartialEq)]
struct Foo {
    bar: i32,
    baz: i32
}
fn main() {
    let g = Foo { bar: 32, baz: 42 };
    let _ = g.bar;
}

in the above code baz is used, even though it isnt;
look at the output from cargo expand (cargo-expand)

struct Foo {
    bar: i32,
    baz: i32,
}
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for Foo {}
#[automatically_derived]
impl ::core::cmp::PartialEq for Foo {
    #[inline]
    fn eq(&self, other: &Foo) -> bool {
        self.bar == other.bar && self.baz == other.baz
    }
}
fn main() {
    let g = Foo { bar: 32, baz: 42 };
    let _ = g.bar;
}

Automatically derived but not ignored

The steps to fix this are probably simple, for example just go to the lint and add the check for a automatically derived PartialEq or PartialOrd

ionicmc-rs

ionicmc-rs commented on Dec 21, 2024

@ionicmc-rs
Contributor

Note: adding A-lints because this issue has to do with the unused lint group

@rusbot label A-lints

ionicmc-rs

ionicmc-rs commented on Dec 21, 2024

@ionicmc-rs
added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Dec 21, 2024
Jules-Bertholet

Jules-Bertholet commented on Jul 5, 2025

@Jules-Bertholet
Contributor

Do we really want to warn here? A field might be read only in PartialEq intentionally, e.g. if it serves as a tag of some sort to make instances of the struct unequal that would be equal otherwise. dead_code should not have false positives like this IMO

smmalis37

smmalis37 commented on Jul 9, 2025

@smmalis37
Contributor

In addition to PartialEq and PartialOrd should this issue also track Hash?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @Wilfred@smmalis37@rustbot@Jules-Bertholet@ionicmc-rs

      Issue actions

        derive(PartialEq) should not prevent "field is never read" warnings · Issue #134588 · rust-lang/rust