allow-unwrap-in-tests does not work in unit test that are also gated by a feature flag

Open
#16,369 1 comment 0 reactions 1 assignee View on GitHub

@corentinguilloteau is already working on this.

Since May 2, 2026.

Assessment

This issue has not been assessed yet.

Description

C-bug
Summary

I have a codebase where I want to prevent the use of unwrap outside of unit test.

For that I tried to use the lint clippy::unwrap_unsed with the configuration allow-unwrap-in-tests=true.

Some tests also depend on a feature flag being activated, i.e., #[cfg(all(test, feature = "my-feature"))]. For those there is a false positive for test helper functions.

Reproducer

Code:

#[cfg(all(test, feature = "my-feature"))]
mod tests {
    fn helper() -> u32 {
        Some(42).unwrap()
    }

    #[test]
    fn test() {
        let _ = helper();
        let _ = Some(21).unwrap();
    }
}

Cargo.toml:

[package]
name = "playground"
version = "0.1.0"
edition = "2024"

[features]
my-feature = []

[lints.clippy]
unwrap_used = "deny"

Current output:

Warning that unwrap() is used in helper()

Desired output:

No warning

Note that the current workaround is to add another cfg(test):

#[cfg(all(test, feature = "my-feature"))]
#[cfg(test)]
mod tests {
//..
}
Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3
Additional Labels

No response

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.