Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive(Default) suppresses warn(dead_code) #98871

Open
CAD97 opened this issue Jul 4, 2022 · 4 comments
Open

derive(Default) suppresses warn(dead_code) #98871

CAD97 opened this issue Jul 4, 2022 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Jul 4, 2022

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.

@CAD97 CAD97 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 4, 2022
@matthiaskrgr
Copy link
Member

Probably a duplicate of #47851

@CAD97
Copy link
Contributor Author

CAD97 commented Jul 4, 2022

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

@compiler-errors
Copy link
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
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants