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

Closed
CAD97 opened this issue Jul 4, 2022 · 4 comments · Fixed by #126302
Closed

derive(Default) suppresses warn(dead_code) #98871

CAD97 opened this issue Jul 4, 2022 · 4 comments · Fixed by #126302
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

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 25, 2024
…ichaelwoerister

Detect unused structs which derived Default

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->

Fixes rust-lang#98871
@bors bors closed this as completed in 58bbade Jun 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 25, 2024
Rollup merge of rust-lang#126302 - mu001999-contrib:ignore/default, r=michaelwoerister

Detect unused structs which derived Default

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->

Fixes rust-lang#98871
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
3 participants