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

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

Closed
mu001999 opened this issue Mar 12, 2024 · 3 comments · Fixed by #122382
Closed

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

mu001999 opened this issue Mar 12, 2024 · 3 comments · Fixed by #122382
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

@mu001999
Copy link
Contributor

mu001999 commented Mar 12, 2024

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

@mu001999 mu001999 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 Mar 12, 2024
@mu001999
Copy link
Contributor Author

@rustbot claim

@krtab
Copy link
Contributor

krtab commented Mar 12, 2024

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

@mu001999
Copy link
Contributor Author

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 ;)

@bors bors closed this as completed in c99b3f2 May 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 23, 2024
Rollup merge of rust-lang#122382 - mu001999:dead_code/enhance, r=petrochenkov

Detect unused structs which implement private traits

Fixes rust-lang#122361
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
Development

Successfully merging a pull request may close this issue.

2 participants