Skip to content

False positive on test-related exception within tests/ #15764

@JP-Ellis

Description

@JP-Ellis

Summary

This relates to those lints in Clippy which have test-related exceptions, for example:

allow-expect-in-tests = true
allow-panic-in-tests  = true
allow-print-in-tests  = true
allow-unwrap-in-tests = true

The issue appears to be that despite these lints being excepted in tests, Clippy still reports on them when used in non-test function within tests/. To my understanding, all code within tests/ is exclusively used for testing and as such, Clippy should allow these exceptions.

I encountered this issue when declaring fixtures using the rstest crate to help reduce some boilerplate, but seems to apply to any function which isn't decorated with a #[test].

Lint Name

multiple

Reproducer

The below files replicate the issue. In particular, when running Clippy with its default targets (which excludes tests/), there's no issue:

cargo clippy
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s

Once the tests are included though, Clippy starts complaining about the lint which should be allowed in tests:

cargo clippy --tests
    Checking mwe v0.1.0 (/Users/joshua.ellis/mwe)
error: used `expect()` on an `Option` value
 --> tests/expect.rs:5:5
  |
5 |     x.expect("Should be Some");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: if this value is `None`, it will panic
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#expect_used
  = note: requested on the command line with `-F clippy::expect-used`

error: could not compile `mwe` (test "expect") due to 1 previous error
Cargo.toml
[package]
name    = "mwe"
version = "0.1.0"

edition = "2024"

[lints.clippy]
expect_used = "forbid"
.clippy.toml
allow-expect-in-tests = true
src/lib.rs
pub fn make_some() -> Option<i32> {
    Some(42)
}

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

    #[test]
    fn expect_is_allowed() {
        make_some().expect("Should be Some");
    }
}
tests/expect.rs
use mwe::make_some;

fn test_helper() -> Option<i32> {
    let x = make_some();
    x.expect("Should be Some");
    x
}

#[test]
fn expect_is_allowed() {
    make_some().expect("Should be Some");
}

#[test]
fn test_helper_works() {
    let result = test_helper();
    assert_eq!(result, Some(42));
}

Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: aarch64-apple-darwin
release: 1.90.0
LLVM version: 20.1.8

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions