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

Dead code lint should include unused default trait methods in the unused chain #118139

Closed
shepmaster opened this issue Nov 21, 2023 · 4 comments · Fixed by #118257
Closed

Dead code lint should include unused default trait methods in the unused chain #118139

shepmaster opened this issue Nov 21, 2023 · 4 comments · Fixed by #118257
Assignees
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

@shepmaster
Copy link
Member

shepmaster commented Nov 21, 2023

Code

enum Category {
    Dead,
    Used,
}

trait Demo {
    fn this_is_unused(&self) -> Category {
        Category::Dead
    }
}

fn main() {
    let _c = Category::Used;
}

Current output

warning: variant `Dead` is never constructed
 --> src/main.rs:2:5
  |
1 | enum Category {
  |      -------- variant in this enum
2 |     Dead,
  |     ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Desired output

(in addition to the existing output)

warning: method `Demo::this_is_unused` is never used
 --> src/lib.rs:LL:CC
  |
L |     fn this_is_unused(&self) -> Category {
  |        ^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Rationale and extra context

When I encountered this lint, I deleted the enum variant, but then got a compiler error as the variant was used within the trait's method. Nowhere did the compiler let me know that it's because the trait's method was unused.

Other cases

No response

Anything else?

It appears that trait methods are never reported as dead code — this example has no warnings:

trait Demo {
    fn unused_1(&self);
    fn unused_2(&self) {}
}

However, I assume something is analyzing to see that the function is unused because otherwise the enum variant couldn't be transitively marked as unused.

@shepmaster shepmaster 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 Nov 21, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 21, 2023
@Urgau
Copy link
Member

Urgau commented Nov 21, 2023

Apparently, not warning on trait items is known and explicit.

// We do not warn trait items.

@shepmaster
Copy link
Member Author

not warning on trait items is [...] explicit.

I half expected that to be the case. I have two parallel thoughts:

  1. Well, why not? I want to know about (non-public) traits and trait methods that I'm not using.
  2. If there is some good reason to not warn about dead trait methods, could we enhance the current message to mention them? For example, "variant Dead is never constructed. It's used in which is also unused"

@mu001999
Copy link
Contributor

@rustbot claim

@shepmaster
Copy link
Member Author

#41883 is the issue about making traits / trait methods detected by the dead code lint.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 26, 2023
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 20, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 20, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 21, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 27, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 28, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 4, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 5, 2024
Make traits / trait methods detected by the dead code lint

Fixes rust-lang#118139 and rust-lang#41883
@bors bors closed this as completed in d4f6f9e Feb 7, 2024
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
5 participants