-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
jyn514
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
[-]derive(PartialEq) shosuld not prevent "field is never read" warnings[/-][+]derive(PartialEq) should not prevent "field is never read" warnings[/+]ionicmc-rs commentedon Dec 21, 2024
Ok ok i reproduced this and it seems only impls of
Debug
,Clone
, andDefault
are ignoredlook at this code:
in the above code
baz
is used, even though it isnt;look at the output from
cargo expand
(cargo-expand)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
orPartialOrd
ionicmc-rs commentedon Dec 21, 2024
Note: adding
A-lints
because this issue has to do with theunused
lint group@rusbot label A-lints
ionicmc-rs commentedon Dec 21, 2024
PartialEq
as#[rustc_trivial_field_reads]
#143487Jules-Bertholet commentedon Jul 5, 2025
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 IMOsmmalis37 commentedon Jul 9, 2025
In addition to
PartialEq
andPartialOrd
should this issue also trackHash
?