Skip to content

dead_code lint highlights more than the ident for impl function #66627

Closed
@alvinhochun

Description

@alvinhochun

This is a follow-up of #58729 and #63064. PR #65830 fixed the issue of too much being highlighted for plain functions, but it appears to not apply to impl functions.

With the following example code:

fn unused() {
    println!("blah");
}

fn unused2(var: i32) {
    println!("foo {}", var);
}

fn unused3(
    var: i32,
) {
    println!("bar {}", var);
}

struct UnusedStruct {
    
}

impl UnusedStruct {
    fn unused_impl_fn_1() {
        println!("blah");
    }

    fn unused_impl_fn_2(var: i32) {
        println!("foo {}", var);
    }

    fn unused_impl_fn_3(
        var: i32,
    ) {
        println!("bar {}", var);
    }
}

fn main() {
    println!("Hello world!");
}

(Playground)

The compiler produces these warnings (on 1.41.0-nightly 2019-11-21 53712f8):

warning: function is never used: `unused`
 --> src/main.rs:1:4
  |
1 | fn unused() {
  |    ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function is never used: `unused2`
 --> src/main.rs:5:4
  |
5 | fn unused2(var: i32) {
  |    ^^^^^^^

warning: function is never used: `unused3`
 --> src/main.rs:9:4
  |
9 | fn unused3(
  |    ^^^^^^^

warning: struct is never constructed: `UnusedStruct`
  --> src/main.rs:15:8
   |
15 | struct UnusedStruct {
   |        ^^^^^^^^^^^^

warning: method is never used: `unused_impl_fn_1`
  --> src/main.rs:20:5
   |
20 |     fn unused_impl_fn_1() {
   |     ^^^^^^^^^^^^^^^^^^^^^

warning: method is never used: `unused_impl_fn_2`
  --> src/main.rs:24:5
   |
24 |     fn unused_impl_fn_2(var: i32) {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: method is never used: `unused_impl_fn_3`
  --> src/main.rs:28:5
   |
28 | /     fn unused_impl_fn_3(
29 | |         var: i32,
30 | |     ) {
31 | |         println!("bar {}", var);
32 | |     }
   | |_____^

This verifies that the aforementioned fix is working for unused1/unused2/unused3, but not for unused_impl_fn_1/unused_impl_fn_2/unused_impl_fn_3.

(pinging @Quantumplation, @estebank)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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