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

nightly: false positive 'dead_code' warning when using trait for the supertrait bounds #121040

Closed
pantos9000 opened this issue Feb 13, 2024 · 4 comments
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

@pantos9000
Copy link

Code

pub struct Foo;

#[cfg(test)]
mod tests_common {
    /// A marker trait that can be used to assert a type is `Send`.
    pub trait AssertSend: Send {}
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::tests_common::AssertSend;

    impl AssertSend for Foo {}
}

Current output

warning: trait `AssertSend` is never used
 --> src/lib.rs:5:15
  |
5 |     pub trait AssertSend: Send {}
  |               ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `test_trait_reproducer` (lib test) generated 1 warning

Desired output

no warning (like in stable)

Rationale and extra context

For the given code, the rust compiler emits a dead_code warning in nightly, but not in stable.

Usually the trait was supposed to be used for multiple modules to ensure Send on certain types. For the sake of simplicity, I put it in the same file. But the issue is also visible across different files.

Other cases

No response

Rust Version

stable version:
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

nightly version:
rustc 1.78.0-nightly (b381d3ab2 2024-02-12)
binary: rustc
commit-hash: b381d3ab27f788f990551100c4425bb782d26d76
commit-date: 2024-02-12
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 17.0.6

Anything else?

No response

@pantos9000 pantos9000 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 Feb 13, 2024
@lcnr
Copy link
Contributor

lcnr commented Feb 13, 2024

this is unrelated to tests

pub struct Foo;

mod nested { 
    /// A marker trait that can be used to assert a type is `Send`.
    pub trait AssertSend: Send {}
    impl AssertSend for super::Foo {}
}

and

pub struct Foo;

/// A marker trait that can be used to assert a type is `Send`.
trait AssertSend: Send {}
impl AssertSend for Foo {}

also lint. we now lint on traits if their impls are never used anywhere #118257

to use a trait pretty much either use the trait as a where-bound somewhere and its associated methods are all unused.

@Noratrieb Noratrieb changed the title nightly: false positive 'dead_code' warning when using trait in tests nightly: false positive 'dead_code' warning when using trait for the supertrait bounds Feb 13, 2024
@Noratrieb
Copy link
Member

That's a clever way of asserting that something is send, I've never seen that before (but I do like it). Usually people just pass their values to a generic function bound on Send. I think it's useful to lint here, because the trait is unused other than in impls, which are basically never a "use", this is very much an exception. You can just #[allow(dead_code)] on the trait definition.

@lcnr
Copy link
Contributor

lcnr commented Feb 13, 2024

as one side note, this may get lowered to a lint at some point. Proving super traits will end up being required when using an impl and there may be cases where specifying the super trait on the impl do not work out. At this point your check (which may be soundness critical?) will not cause a compilation error anymore

please use

const fn impls_trait<T: Trait>() {}

fn test() {
    impls_trait::<MyType>()
}

@pantos9000
Copy link
Author

Okay, thanks a lot for elaborating. Was first thinking this could be a regression, but now I see it actually is an improvement :)

Closing the issue.

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

No branches or pull requests

3 participants