Skip to content

dead_code warning when private trait is only used as type bound in another trait #128839

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

Closed
eduardosm opened this issue Aug 8, 2024 · 1 comment · Fixed by #141407
Closed
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

@eduardosm
Copy link
Contributor

Code

trait UInt: Copy + From<u8> {}

impl UInt for u16 {}

trait Int: Copy {
    type Unsigned: UInt;

    fn as_unsigned(self) -> Self::Unsigned;
}

impl Int for i16 {
    type Unsigned = u16;

    fn as_unsigned(self) -> u16 {
        self as _
    }
}

fn priv_func<T: Int>(x: u8, y: T) -> (T::Unsigned, T::Unsigned) {
    (T::Unsigned::from(x), y.as_unsigned())
}

pub fn pub_func(x: u8, y: i16) -> (u16, u16) {
    priv_func(x, y)
}

Current output

warning: trait `UInt` is never used
 --> src/lib.rs:1:7
  |
1 | trait UInt: Copy + From<u8> {}
  |       ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Desired output

<empty>

Rationale and extra context

The false positive happens when a private trait is only used as type bound in another trait.

Other cases

No response

Rust Version

rustc 1.80.1 (3f5fd8dd4 2024-08-06)
binary: rustc
commit-hash: 3f5fd8dd41153bc5fdca9427e9e05be2c767ba23
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7

Anything else?

No response

@eduardosm eduardosm 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 Aug 8, 2024
@compiler-errors
Copy link
Member

This uncovers a regression from 1.78 in #118257, since we backported recent changes to dead code analysis. cc @mu001999

@bors bors closed this as completed in 896be66 May 30, 2025
rust-timer added a commit that referenced this issue May 30, 2025
Rollup merge of #141407 - mu001999-contrib:dead-code/refactor, r=petrochenkov

Refactor the two-phase check for impls and impl items

Refactor the two-phase dead code check to make the logic clearer and simpler:
1. adding assoc fn and impl into `unsolved_items` directly during the initial construction of the worklist
2. converge the logic of checking whether assoc fn and impl are used to `item_should_be_checked`, and the item is considered used only when its corresponding trait and Self adt are used

This PR only refactors as much as possible to avoid affecting the original functions. However, due to the adjustment of the order of checks, the test results are slightly different, but overall, there is no regression problem

Fixes #127911
Fixes #128839

Extracted from #128637.
r? petrochenkov

try-job: dist-aarch64-linux
github-actions bot pushed a commit to rust-lang/miri that referenced this issue May 31, 2025
…ochenkov

Refactor the two-phase check for impls and impl items

Refactor the two-phase dead code check to make the logic clearer and simpler:
1. adding assoc fn and impl into `unsolved_items` directly during the initial construction of the worklist
2. converge the logic of checking whether assoc fn and impl are used to `item_should_be_checked`, and the item is considered used only when its corresponding trait and Self adt are used

This PR only refactors as much as possible to avoid affecting the original functions. However, due to the adjustment of the order of checks, the test results are slightly different, but overall, there is no regression problem

Fixes rust-lang/rust#127911
Fixes rust-lang/rust#128839

Extracted from rust-lang/rust#128637.
r? petrochenkov

try-job: dist-aarch64-linux
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
2 participants