Skip to content

Lint regression: dead_code ignores #[allow(dead_code)] on traits #144060

@tgross35

Description

@tgross35

Code

#[allow(dead_code)]
trait Foo {
    const FOO: u32;
}

impl Foo for u32 {
    const FOO: u32 = roundtrip(0);
}

const fn roundtrip(x: u32) -> u32 {
    x
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=af400bf3a25f81641de319af6c598ffe

Current output

warning: function `foo` is never used
  --> src/lib.rs:10:10
   |
10 | const fn foo(x: u32) -> u32 {
   |          ^^^
   |
   = note: `#[warn(dead_code)]` on by default

Desired output

No lint

Rationale and extra context

roundtrip is used in an instance of trait Foo so it isn't dead. Foo isn't used, but with it being allowed I don't think the check should propagate back elsewhere.

E.g. bar doesn't get a lint in this case:

#[allow(dead_code)]
const X: u32 = bar(0);

const fn bar(x: u32) -> u32 {
    x
}

This regressed in #143519, cc @mu001999

Rust Version

searched nightlies: from nightly-2025-05-31 to nightly-2025-07-15
regressed nightly: nightly-2025-07-14
searched commit range: bfc046a...e9182f1
regressed commit: 56835d7

bisected with cargo-bisect-rustc v0.6.10

Host triple: aarch64-apple-darwin
Reproduce with:

cargo bisect-rustc --start 2025-05-31 --end 2025-07-15 --without-cargo --script=rustc -- a.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsL-false-positiveLint: False positive (should not have fired).P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions