rust-lang / rust-lang/rust

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

Open
#134,588 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lints T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no source files or tests. Reproduce the warning behavior with the provided example on rustc 1.83, then review the prior discussions in issues #84647 and #85200 before locating the dead-code diagnostic implementation. Done means unused-field warnings appear for both structs despite the PartialEq derive.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.