Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
talbenari1 opened this issue May 1, 2022 · 1 comment · Fixed by #96651
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@talbenari1
Copy link

talbenari1 commented May 1, 2022

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.

@talbenari1 talbenari1 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 1, 2022
@ken-matsui
Copy link
Contributor

@rustbot claim

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants