Skip to content

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

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 12, 2024
  2. mu001999 commented on Mar 12, 2024

    @mu001999
    MemberAuthor

    @rustbot claim

  3. krtab commented on Mar 12, 2024

    @krtab
    Contributor

    Maybe you can get inspiration from #119552, especially if you end up having to fix previously undetected errors in multiple places in rustc.

  4. mu001999 commented on Mar 12, 2024

    @mu001999
    MemberAuthor

    Maybe you can get inspiration from #119552, especially if you end up having to fix previously undetected errors in multiple places in rustc.

    Thanks for your suggestion, but I think this just needs to extend #121752 ;)

  5. added a commit that references this issue on May 23, 2024
    c99b3f2
  6. added a commit that references this issue on May 23, 2024
    4cfe57a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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