rust-lang / rust-lang/rust

`cfg(test)` is not recognized in `[cfg(any(test, feature = ... ))]`

Open
#137,691 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub fn foo() {
    println!("Hello, world!");
}

#[cfg(any(test, feature = "test"))]
mod test {
    use super::foo;

    #[test]
    fn test_foo() {
        foo()
    }
}
Current output
warning: unused import: `super::foo`
 --> src/lib.rs:7:9
  |
7 |     use super::foo;
  |         ^^^^^^^^^^
  |
help: if this is a test module, consider adding a `#[cfg(test)]` to the containing module
 --> src/lib.rs:6:1
  |
6 | mod test {
  | ^^^^^^^^
  = note: `#[warn(unused_imports)]` on by default

warning: `<cratename>` (lib) generated 1 warning (run `cargo fix --lib -p <cratename>` to apply 1 suggestion)
Desired output
warning: unused import: `super::foo`
 --> src/lib.rs:7:9
  |
7 |     use super::foo;
  |         ^^^^^^^^^^
  |
help: on the containing module, consider changing `#[cfg(any(test, ... ))]` to `#[cfg(test)]`
 --> src/lib.rs:6:1
  |
6 | mod test {
  | ^^^^^^^^
  = note: `#[warn(unused_imports)]` on by default

warning: `<cratename>` (lib) generated 1 warning (run `cargo fix --lib -p <cratename>` to apply 1 suggestion)
Rationale and extra context

Previously reported as clippy-fix issue and then as a cargo issue.

The suggestion of adding #[cfg(test)] is not technically wrong (adding that above the cfg(any...) does indeed suppress the warning), but the problem isn't that the cfg is missing (as the help-text assumes) but that it's overly broad.

Checking whether there's a #[cfg(any(...test...))] before generating the helptext (as I've suggested above in the "desired output") would be the maximally-correct thing to do, but it also seems like it could be a lot of effort for a weird edge-case. As an alternative, the wording of the helptext could be adjusted to be compatible with the possibility that the module is already annotated, but incorrectly:

help: if this is a test module, ensure that the containing module is annotated with `#[cfg(test)]`
Other cases

Rust Version
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7
Anything else?

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

Reproduce the provided Rust example with rustc 1.85.0 and inspect the compiler diagnostic that suggests adding #[cfg(test)]. Read the diagnostic logic and existing related tests, if present. Done means the warning no longer implies that the cfg annotation is missing when it is already present in a broader form, with the chosen wording covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.