Skip to content

incompatible_msrv does not work with unit tests #15369

@axelkar

Description

@axelkar

Summary

incompatible_msrv does not seem to work with unit tests when it's gated to cfg(test), even with --tests.

The documentation of the lint recommend using inner attributes, I'm using outer because I'm on Rust stable, but it shouldn't make any difference.

#![cfg_attr(test, clippy::msrv = "1.85.0")]

Reproducer

See comments:

#[cfg(test)]
// I was going to set this higher from the library's own MSRV but I wanted to see that it worked
#[clippy::msrv = "1.60.0"]
mod tests {
    use std::sync::LazyLock;

    static _FOO: LazyLock<()> = LazyLock::new(|| ()); // Clippy lints as clippy::incompatible_msrv only without #[cfg(test)]
    static _BAR: () = {
        (); // Clippy lints as clippy::no_effect with and without #[cfg(test)]
    };

    #[test]
    fn bar() {
        LazyLock::new(|| ()); // No lint even without #[cfg(test)]
        (); // Clippy lints as clippy::no_effect with and without #[cfg(test)]
    }
}

Version

rustc 1.87.0 (17067e9ac 2025-05-09) (built from a source tarball)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.6

Additional Labels

@rustbot label +I-false-negative
@rustbot label +L-tests
@rustbot label +L-suspicious

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tL-suspiciousLint: Belongs in the suspicious lint groupL-testsLint: Lints test code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions