Skip to content

Commit

Permalink
Rollup merge of #96651 - ken-matsui:omit-unnecessary-help-to-add-cfg-…
Browse files Browse the repository at this point in the history
…test, r=cjgillot

Omit unnecessary help to add `#[cfg(test)]` when already annotated

Closes: #96611

The related PR is: #91770
  • Loading branch information
JohnTitor authored May 17, 2022
2 parents ce49ce2 + 03007de commit 78c709c
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 14 deletions.
35 changes: 21 additions & 14 deletions compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,28 @@ impl Resolver<'_> {
"remove the unused import"
};

let parent_module = visitor.r.get_nearest_non_block_module(
visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
);
let test_module_span = match module_to_string(parent_module) {
Some(module)
if module == "test"
|| module == "tests"
|| module.starts_with("test_")
|| module.starts_with("tests_")
|| module.ends_with("_test")
|| module.ends_with("_tests") =>
{
Some(parent_module.span)
// If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]`
// attribute; however, if not, suggest adding the attribute. There is no way to
// retrieve attributes here because we do not have a `TyCtxt` yet.
let test_module_span = if visitor.r.session.opts.test {
None
} else {
let parent_module = visitor.r.get_nearest_non_block_module(
visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
);
match module_to_string(parent_module) {
Some(module)
if module == "test"
|| module == "tests"
|| module.starts_with("test_")
|| module.starts_with("tests_")
|| module.ends_with("_test")
|| module.ends_with("_tests") =>
{
Some(parent_module.span)
}
_ => None,
}
_ => None,
};

visitor.r.lint_buffer.buffer_lint_with_diagnostic(
Expand Down
84 changes: 84 additions & 0 deletions src/test/ui/imports/unused-imports-in-test-mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// compile-flags: --test

#![deny(unused_imports)]

use std::io::BufRead; //~ ERROR unused import: `std::io::BufRead`

fn a() {}
fn b() {}

mod test {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod tests {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod test_a {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod a_test {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod tests_a {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod a_tests {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod fastest_search {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

#[cfg(test)]
mod test_has_attr {
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

mod test_has_no_attr {
#[cfg(test)]
use super::a; //~ ERROR unused import: `super::a`

fn foo() {
use crate::b; //~ ERROR unused import: `crate::b`
}
}

fn main() {}
122 changes: 122 additions & 0 deletions src/test/ui/imports/unused-imports-in-test-mode.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
error: unused import: `std::io::BufRead`
--> $DIR/unused-imports-in-test-mode.rs:5:5
|
LL | use std::io::BufRead;
| ^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/unused-imports-in-test-mode.rs:3:9
|
LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:11:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:14:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:19:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:22:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:27:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:30:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:35:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:38:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:43:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:46:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:51:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:54:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:59:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:62:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:68:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:71:13
|
LL | use crate::b;
| ^^^^^^^^

error: unused import: `super::a`
--> $DIR/unused-imports-in-test-mode.rs:77:9
|
LL | use super::a;
| ^^^^^^^^

error: unused import: `crate::b`
--> $DIR/unused-imports-in-test-mode.rs:80:13
|
LL | use crate::b;
| ^^^^^^^^

error: aborting due to 19 previous errors

0 comments on commit 78c709c

Please sign in to comment.