Skip to content

Compiler emits invalid help diagnostic when unused import is found in test module #96611

Closed
@talbenari1

Description

@talbenari1

Given a #[cfg(test)]-annotated module that contains an unused import, like so: (Rust Playground)

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    #[test]
    fn test_empty() {}
}

The compiler outputs a warning and corresponding help message like this:

   Compiling playground v0.0.1 (/playground)
warning: unused import: `std::collections::HashSet`
 --> src/lib.rs:3:9
  |
3 |     use std::collections::HashSet;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default
help: consider adding a `#[cfg(test)]` to the containing module
 --> src/lib.rs:2:1
  |
2 | mod tests {
  | ^^^^^^^^^

warning: `playground` (lib test) generated 1 warning
    Finished test [unoptimized + debuginfo] target(s) in 5.11s

Obviously the help is erroneous because the annotation it's recommending is already there. I assume the logic determining whether to add the help message naively assumes that "unused import warning" + "test module" + "reference to unused import's value inside of test module" = "help message", in which case it should be revised to consider whether the test module's reference is unused as well.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions