Skip to content

derive(Default) suppresses warn(dead_code) #98871

Closed
@CAD97

Description

@CAD97
Contributor

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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jul 4, 2022
matthiaskrgr

matthiaskrgr commented on Jul 4, 2022

@matthiaskrgr
Member

Probably a duplicate of #47851

CAD97

CAD97 commented on Jul 4, 2022

@CAD97
ContributorAuthor

Definitely related but maybe not a duplicate; #47851 is about explicit impls whereas this is a subset for just #[automatically_derived] impls.

compiler-errors

compiler-errors commented on Jul 4, 2022

@compiler-errors
Member

I thought the dead code lint (or other lints?) actually mentioned they didn't do this explicitly. Like, "this lint ignores automatic derives" or something.

compiler-errors

compiler-errors commented on Jul 4, 2022

@compiler-errors
Member

Ah, unused field lint does:

note: `Foo` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
added a commit that references this issue on Jun 25, 2024

Auto merge of rust-lang#126302 - mu001999-contrib:ignore/default, r=m…

0521fe5
added a commit that references this issue on Jun 25, 2024

Rollup merge of rust-lang#126302 - mu001999-contrib:ignore/default, r…

58bbade
added a commit that references this issue on Jun 25, 2024
f37fcce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @matthiaskrgr@compiler-errors@CAD97

      Issue actions

        derive(Default) suppresses warn(dead_code) · Issue #98871 · rust-lang/rust