Skip to content

Commit 03007de

Browse files
committed
Omit unnecessary help to add #[cfg(test)] when already annotated
1 parent 4dd8b42 commit 03007de

File tree

3 files changed

+227
-14
lines changed

3 files changed

+227
-14
lines changed

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,28 @@ impl Resolver<'_> {
315315
"remove the unused import"
316316
};
317317

318-
let parent_module = visitor.r.get_nearest_non_block_module(
319-
visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
320-
);
321-
let test_module_span = match module_to_string(parent_module) {
322-
Some(module)
323-
if module == "test"
324-
|| module == "tests"
325-
|| module.starts_with("test_")
326-
|| module.starts_with("tests_")
327-
|| module.ends_with("_test")
328-
|| module.ends_with("_tests") =>
329-
{
330-
Some(parent_module.span)
318+
// If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]`
319+
// attribute; however, if not, suggest adding the attribute. There is no way to
320+
// retrieve attributes here because we do not have a `TyCtxt` yet.
321+
let test_module_span = if visitor.r.session.opts.test {
322+
None
323+
} else {
324+
let parent_module = visitor.r.get_nearest_non_block_module(
325+
visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
326+
);
327+
match module_to_string(parent_module) {
328+
Some(module)
329+
if module == "test"
330+
|| module == "tests"
331+
|| module.starts_with("test_")
332+
|| module.starts_with("tests_")
333+
|| module.ends_with("_test")
334+
|| module.ends_with("_tests") =>
335+
{
336+
Some(parent_module.span)
337+
}
338+
_ => None,
331339
}
332-
_ => None,
333340
};
334341

335342
visitor.r.lint_buffer.buffer_lint_with_diagnostic(
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// compile-flags: --test
2+
3+
#![deny(unused_imports)]
4+
5+
use std::io::BufRead; //~ ERROR unused import: `std::io::BufRead`
6+
7+
fn a() {}
8+
fn b() {}
9+
10+
mod test {
11+
use super::a; //~ ERROR unused import: `super::a`
12+
13+
fn foo() {
14+
use crate::b; //~ ERROR unused import: `crate::b`
15+
}
16+
}
17+
18+
mod tests {
19+
use super::a; //~ ERROR unused import: `super::a`
20+
21+
fn foo() {
22+
use crate::b; //~ ERROR unused import: `crate::b`
23+
}
24+
}
25+
26+
mod test_a {
27+
use super::a; //~ ERROR unused import: `super::a`
28+
29+
fn foo() {
30+
use crate::b; //~ ERROR unused import: `crate::b`
31+
}
32+
}
33+
34+
mod a_test {
35+
use super::a; //~ ERROR unused import: `super::a`
36+
37+
fn foo() {
38+
use crate::b; //~ ERROR unused import: `crate::b`
39+
}
40+
}
41+
42+
mod tests_a {
43+
use super::a; //~ ERROR unused import: `super::a`
44+
45+
fn foo() {
46+
use crate::b; //~ ERROR unused import: `crate::b`
47+
}
48+
}
49+
50+
mod a_tests {
51+
use super::a; //~ ERROR unused import: `super::a`
52+
53+
fn foo() {
54+
use crate::b; //~ ERROR unused import: `crate::b`
55+
}
56+
}
57+
58+
mod fastest_search {
59+
use super::a; //~ ERROR unused import: `super::a`
60+
61+
fn foo() {
62+
use crate::b; //~ ERROR unused import: `crate::b`
63+
}
64+
}
65+
66+
#[cfg(test)]
67+
mod test_has_attr {
68+
use super::a; //~ ERROR unused import: `super::a`
69+
70+
fn foo() {
71+
use crate::b; //~ ERROR unused import: `crate::b`
72+
}
73+
}
74+
75+
mod test_has_no_attr {
76+
#[cfg(test)]
77+
use super::a; //~ ERROR unused import: `super::a`
78+
79+
fn foo() {
80+
use crate::b; //~ ERROR unused import: `crate::b`
81+
}
82+
}
83+
84+
fn main() {}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
error: unused import: `std::io::BufRead`
2+
--> $DIR/unused-imports-in-test-mode.rs:5:5
3+
|
4+
LL | use std::io::BufRead;
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-imports-in-test-mode.rs:3:9
9+
|
10+
LL | #![deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: unused import: `super::a`
14+
--> $DIR/unused-imports-in-test-mode.rs:11:9
15+
|
16+
LL | use super::a;
17+
| ^^^^^^^^
18+
19+
error: unused import: `crate::b`
20+
--> $DIR/unused-imports-in-test-mode.rs:14:13
21+
|
22+
LL | use crate::b;
23+
| ^^^^^^^^
24+
25+
error: unused import: `super::a`
26+
--> $DIR/unused-imports-in-test-mode.rs:19:9
27+
|
28+
LL | use super::a;
29+
| ^^^^^^^^
30+
31+
error: unused import: `crate::b`
32+
--> $DIR/unused-imports-in-test-mode.rs:22:13
33+
|
34+
LL | use crate::b;
35+
| ^^^^^^^^
36+
37+
error: unused import: `super::a`
38+
--> $DIR/unused-imports-in-test-mode.rs:27:9
39+
|
40+
LL | use super::a;
41+
| ^^^^^^^^
42+
43+
error: unused import: `crate::b`
44+
--> $DIR/unused-imports-in-test-mode.rs:30:13
45+
|
46+
LL | use crate::b;
47+
| ^^^^^^^^
48+
49+
error: unused import: `super::a`
50+
--> $DIR/unused-imports-in-test-mode.rs:35:9
51+
|
52+
LL | use super::a;
53+
| ^^^^^^^^
54+
55+
error: unused import: `crate::b`
56+
--> $DIR/unused-imports-in-test-mode.rs:38:13
57+
|
58+
LL | use crate::b;
59+
| ^^^^^^^^
60+
61+
error: unused import: `super::a`
62+
--> $DIR/unused-imports-in-test-mode.rs:43:9
63+
|
64+
LL | use super::a;
65+
| ^^^^^^^^
66+
67+
error: unused import: `crate::b`
68+
--> $DIR/unused-imports-in-test-mode.rs:46:13
69+
|
70+
LL | use crate::b;
71+
| ^^^^^^^^
72+
73+
error: unused import: `super::a`
74+
--> $DIR/unused-imports-in-test-mode.rs:51:9
75+
|
76+
LL | use super::a;
77+
| ^^^^^^^^
78+
79+
error: unused import: `crate::b`
80+
--> $DIR/unused-imports-in-test-mode.rs:54:13
81+
|
82+
LL | use crate::b;
83+
| ^^^^^^^^
84+
85+
error: unused import: `super::a`
86+
--> $DIR/unused-imports-in-test-mode.rs:59:9
87+
|
88+
LL | use super::a;
89+
| ^^^^^^^^
90+
91+
error: unused import: `crate::b`
92+
--> $DIR/unused-imports-in-test-mode.rs:62:13
93+
|
94+
LL | use crate::b;
95+
| ^^^^^^^^
96+
97+
error: unused import: `super::a`
98+
--> $DIR/unused-imports-in-test-mode.rs:68:9
99+
|
100+
LL | use super::a;
101+
| ^^^^^^^^
102+
103+
error: unused import: `crate::b`
104+
--> $DIR/unused-imports-in-test-mode.rs:71:13
105+
|
106+
LL | use crate::b;
107+
| ^^^^^^^^
108+
109+
error: unused import: `super::a`
110+
--> $DIR/unused-imports-in-test-mode.rs:77:9
111+
|
112+
LL | use super::a;
113+
| ^^^^^^^^
114+
115+
error: unused import: `crate::b`
116+
--> $DIR/unused-imports-in-test-mode.rs:80:13
117+
|
118+
LL | use crate::b;
119+
| ^^^^^^^^
120+
121+
error: aborting due to 19 previous errors
122+

0 commit comments

Comments
 (0)