Skip to content

Improve dead_code lint to detect unused structs which implement private traits #122361

Closed
@mu001999

Description

@mu001999

Code

#![deny(dead_code)]

struct T;
struct UnusedT1;
struct UnusedT2;

pub trait PubTrait {
    fn foo(&self) -> Self;
}

impl PubTrait for T {
    fn foo(&self) -> Self { T }
}

impl PubTrait for UnusedT1 {
    fn foo(&self) -> Self { UnusedT1 }
}


trait PriTrait {
    fn foo(&self) -> Self;
}

impl PriTrait for T {
    fn foo(&self) -> Self { T }
}

impl PriTrait for UnusedT2 {
    fn foo(&self) -> Self { UnusedT2 }
}

fn main() {
    let t = T;
    let _t = <T as PubTrait>::foo(&t);
    let _t = <T as PriTrait>::foo(&t);
}

Current output

error: struct `UnusedT1` is never constructed
 --> src/main.rs:4:8
  |
4 | struct UnusedT1;
  |        ^^^^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

error: struct `UnusedT1` is never constructed
 --> src/main.rs:4:8
  |
4 | struct UnusedT1;
  |        ^^^^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(dead_code)]
  |         ^^^^^^^^^

error: struct `UnusedT2` is never constructed
 --> src/main.rs:5:8
  |
5 | struct UnusedT2;
  |        ^^^^^^^^

error: could not compile `playground` (bin "playground") due to 2 previous errors

Rationale and extra context

No response

Other cases

No response

Rust Version

Rustc 1.78.0-nightly (2024-03-11 4a0cc881dcc4d800f106)

Anything else?

No response

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