Skip to content

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

Closed
@shepmaster

Description

@shepmaster

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.

Metadata

Metadata

Assignees

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