rust-lang / rust-lang/rust-clippy

`tests_outside_test_module` is triggered when using "complex" `cfg` attributes

Open
#16,476 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Using a "complex" cfg attribute like #[cfg(all(test))] on a module that contains a function with the test attribute causes tests_outside_test_module to incorrectly fire. In my case I know the tests in the module will panic due to OOM when using a 16-bit architecture since more than i16::MAX bytes is needed, so my actual example was using #[cfg(all(test, not(target_pointer_width = "16")))].

Lint Name

tests_outside_test_module

Reproducer

I tried this code:

#[deny(clippy::tests_outside_test_module, reason = "example")]
#[expect(clippy::non_minimal_cfg, reason = "example")]
#[cfg(all(test))]
mod tests {
    #[test]
    fn foo() {}
}

I saw this happen:

error: this function marked with #[test] is outside a #[cfg(test)] module
 --> src/lib.rs:6:5
  |
6 |     fn foo() {}
  |     ^^^^^^^^^^^
  |
  = note: move it to a testing module marked with #[cfg(test)]
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#tests_outside_test_module
  = note: example
note: the lint level is defined here
 --> src/lib.rs:1:8
  |
1 | #[deny(clippy::tests_outside_test_module, reason = "example")]
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `example` (lib test) due to 1 previous error

I expected to see this happen: no lints to fire

Version
rustc 1.93.0 (254b59607 2026-01-19)
binary: rustc
commit-hash: 254b59607d4417e9dffbc307138ae5c86280fe4c
commit-date: 2026-01-19
host: x86_64-unknown-linux-gnu
release: 1.93.0
LLVM version: 21.1.8
Additional Labels

No response

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.

Research direction

Start with the tests_outside_test_module lint and reproduce the report's src/lib.rs example using #[cfg(all(test))] and the nested #[test] function. Compare the lint's handling of simple and complex cfg attributes; done means the reproducer emits no lint while the lint still catches genuinely unguarded test functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.