Skip to content

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

Open
@peterjoel

Description

@peterjoel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions