Skip to content

False positive dead_code for const used as const generic argument during impl marker trait #128617

Open
@datdenkikniet

Description

@datdenkikniet

In the following snippet, CONST gets a dead_code warning. However, CONST is most definitely used and the trait is also pub, so this warning seems incorrect.

Removing CONST causes the crate to no longer compile, and removing the impl would be a breaking change.

Adding any function (i.e. fn method();) to the trait and implementing it removes the warning as well. However, for marker traits this is not an option. It also clearly demonstrates that CONST is considered to be used despite being unaffected by fn method().

Rust version: rustc 1.80.0 (0514789 2024-07-21)

This code compiles without warnings on 1.79.0 (rustc 1.79.0 (129f3b9 2024-06-10))

pub struct Value<const NUMBER: u8> {}

pub trait Trait {}

const CONST: u8 = 11;

impl Trait for Value<CONST> {}

Warning:

warning: constant `CONST` is never used
 --> lib.rs:5:7
  |
5 | const CONST: u8 = 11;
  |       ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Adding method() removes the warning:

pub struct Value<const NUMBER: u8> {}

pub trait Trait {
    fn method();
}

const CONST: u8 = 11;

impl Trait for Value<CONST> {
    fn method() {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-dead_codeLint: dead_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions