rust-lang / rust-lang/rust-clippy

dead_code lint is triggered when a field is only used in Debug impls

Open
#8,353 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I upgraded our project from 1.56.1 to 1.58.1 and got a lot of new warnings about dead code because fields are never read. It seems that the fields in question are used only in Debug impls. This is still a usage so the lint should not trigger.

Lint Name

dead_code

Reproducer

I tried this code:

#[derive(Debug)]
struct Foo {
    a: &'static str,
}

fn main() {
    let foo = Foo { a: "hello" };
    println!("{:?}", foo);
}

I saw this happen:

warning: field is never read: `a`
 --> src/main.rs:4:5
  |
4 |     a: &'static str,
  |     ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

I expected this to happen:

No warnings.

Version
rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-unknown-linux-gnu
release: 1.58.1
LLVM version: 13.0.0
Additional Labels

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

Run the supplied Rust reproducer with the affected compiler versions and inspect how dead_code handles fields used only by Debug implementations. Add a regression test for the derive(Debug) case and verify that it produces no dead_code warning while unrelated unused fields remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.