Skip to content

unused_imports lint help on module named tests_* too assertive and potentially confusing #121502

Closed
@jieyouxu

Description

@jieyouxu

Code

mod tests_revision_unpaired_stdout_stderr {
    use std::ptr;
}

fn main() {}

Current output

warning: unused import: `std::ptr`
 --> src/main.rs:2:9
  |
2 |     use std::ptr;
  |         ^^^^^^^^
  |
help: consider adding a `#[cfg(test)]` to the containing module
 --> src/main.rs:1:1
  |
1 | mod tests_revision_unpaired_stdout_stderr {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: `#[warn(unused_imports)]` on by default

Desired output

warning: unused import: `std::ptr`
 --> src/main.rs:2:9
  |
2 |     use std::ptr;
  |         ^^^^^^^^
  |
help: if this is a test module, consider adding a `#[cfg(test)]` to the containing module
 --> src/main.rs:1:1
  |
1 | mod tests_revision_unpaired_stdout_stderr {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: `#[warn(unused_imports)]` on by default

Rationale and extra context

This help message seems weird. I'd expect the help to be more like:

  • Consider adding #[allow(unused_imports)] if you want to silent the lint on the unused import
  • Consider removing the unused import

Suggesting to add #[cfg(test)] to the module is the least expected option, and it seems too assertive in its current wording.

Other cases

No response

Rust Version

rustc 1.78.0-nightly (bb594538f 2024-02-20)
binary: rustc
commit-hash: bb594538fc6e84213a6b8d5e165442570aa48923
commit-date: 2024-02-20
host: x86_64-apple-darwin
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

Perhaps the detection for triggering #[cfg(test)] on the module could be smarter and only consider a module as a potential test module if it contains at least one #[test] annotated function?

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-confusingDiagnostics: Confusing error or lint that should be reworked.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-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