Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Wilfred opened this issue Dec 20, 2024 · 3 comments
Open

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

Wilfred opened this issue Dec 20, 2024 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Dec 20, 2024

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

@Wilfred Wilfred added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 20, 2024
@Wilfred Wilfred changed the title derive(PartialEq) shosuld not prevent "field is never read" warnings derive(PartialEq) should not prevent "field is never read" warnings Dec 20, 2024
@ionicmc-rs
Copy link
Contributor

ionicmc-rs commented Dec 21, 2024

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
Copy link
Contributor

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

@rusbot label A-lints

@ionicmc-rs

This comment has been minimized.

@rustbot rustbot added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants