Skip to content

derive(Default) suppresses warn(dead_code) #98871

@CAD97

Description

@CAD97

Given the following code: [playground]

#[derive(Default)] struct T {} struct U {}

The current output is:

warning: struct `U` is never constructed --> src/lib.rs:4:8 | 4 | struct U {} | ^ | = note: `#[warn(dead_code)]` on by default 

Ideally the output should look like:

warning: struct `T` is never constructed --> src/lib.rs:2:8 | 2 | struct T {} | ^ | = note: `#[warn(dead_code)]` on by default warning: struct `U` is never constructed --> src/lib.rs:4:8 | 4 | struct U {} | ^ 

After the derive, the code looks roughly like

struct T {} #[automatically_derived] impl Default for T { #[inline] fn default() -> T { T {} } } struct U {}

As the default implementation is both 1) itself unused and 2) #[automatically_derived], ideally it should not count as a use for suppressing the dead_code lint on T.

The derived implementations for Clone, Debug, PartialEq, and Hash also show this behavior.

I seem to recall a previous change to #[automatically_derived] to change the interaction between the unused code lint and derived implementations, but could not find it offhand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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