Skip to content

Commit c597dc8

Browse files
committed
show a suggestion in case module starts_with/ends_with "test"
1 parent 3e43de4 commit c597dc8

File tree

3 files changed

+161
-4
lines changed

3 files changed

+161
-4
lines changed

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,16 @@ impl Resolver<'_> {
320320
visitor.r.local_def_id(unused.use_tree_id).to_def_id(),
321321
);
322322
let test_module_span = match module_to_string(parent_module) {
323-
Some(module) if module.contains("test") => Some(parent_module.span),
323+
Some(module)
324+
if module == "test"
325+
|| module == "tests"
326+
|| module.starts_with("test_")
327+
|| module.starts_with("tests_")
328+
|| module.ends_with("_test")
329+
|| module.ends_with("_tests") =>
330+
{
331+
Some(parent_module.span)
332+
}
324333
_ => None,
325334
};
326335

src/test/ui/imports/unused-imports-in-test-module.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ mod test {
1313
}
1414
}
1515

16+
mod tests {
17+
use super::a; //~ ERROR unused import: `super::a`
18+
19+
fn foo() {
20+
use crate::b; //~ ERROR unused import: `crate::b`
21+
}
22+
}
23+
1624
mod test_a {
1725
use super::a; //~ ERROR unused import: `super::a`
1826

@@ -21,4 +29,36 @@ mod test_a {
2129
}
2230
}
2331

32+
mod a_test {
33+
use super::a; //~ ERROR unused import: `super::a`
34+
35+
fn foo() {
36+
use crate::b; //~ ERROR unused import: `crate::b`
37+
}
38+
}
39+
40+
mod tests_a {
41+
use super::a; //~ ERROR unused import: `super::a`
42+
43+
fn foo() {
44+
use crate::b; //~ ERROR unused import: `crate::b`
45+
}
46+
}
47+
48+
mod a_tests {
49+
use super::a; //~ ERROR unused import: `super::a`
50+
51+
fn foo() {
52+
use crate::b; //~ ERROR unused import: `crate::b`
53+
}
54+
}
55+
56+
mod fastest_search {
57+
use super::a; //~ ERROR unused import: `super::a`
58+
59+
fn foo() {
60+
use crate::b; //~ ERROR unused import: `crate::b`
61+
}
62+
}
63+
2464
fn main() {}

src/test/ui/imports/unused-imports-in-test-module.stderr

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ LL | use super::a;
4343
help: consider adding a `#[cfg(test)]` to the parent module
4444
--> $DIR/unused-imports-in-test-module.rs:16:1
4545
|
46-
LL | mod test_a {
47-
| ^^^^^^^^^^
46+
LL | mod tests {
47+
| ^^^^^^^^^
4848

4949
error: unused import: `crate::b`
5050
--> $DIR/unused-imports-in-test-module.rs:20:13
@@ -55,8 +55,116 @@ LL | use crate::b;
5555
help: consider adding a `#[cfg(test)]` to the parent module
5656
--> $DIR/unused-imports-in-test-module.rs:16:1
5757
|
58+
LL | mod tests {
59+
| ^^^^^^^^^
60+
61+
error: unused import: `super::a`
62+
--> $DIR/unused-imports-in-test-module.rs:25:9
63+
|
64+
LL | use super::a;
65+
| ^^^^^^^^
66+
|
67+
help: consider adding a `#[cfg(test)]` to the parent module
68+
--> $DIR/unused-imports-in-test-module.rs:24:1
69+
|
70+
LL | mod test_a {
71+
| ^^^^^^^^^^
72+
73+
error: unused import: `crate::b`
74+
--> $DIR/unused-imports-in-test-module.rs:28:13
75+
|
76+
LL | use crate::b;
77+
| ^^^^^^^^
78+
|
79+
help: consider adding a `#[cfg(test)]` to the parent module
80+
--> $DIR/unused-imports-in-test-module.rs:24:1
81+
|
5882
LL | mod test_a {
5983
| ^^^^^^^^^^
6084

61-
error: aborting due to 5 previous errors
85+
error: unused import: `super::a`
86+
--> $DIR/unused-imports-in-test-module.rs:33:9
87+
|
88+
LL | use super::a;
89+
| ^^^^^^^^
90+
|
91+
help: consider adding a `#[cfg(test)]` to the parent module
92+
--> $DIR/unused-imports-in-test-module.rs:32:1
93+
|
94+
LL | mod a_test {
95+
| ^^^^^^^^^^
96+
97+
error: unused import: `crate::b`
98+
--> $DIR/unused-imports-in-test-module.rs:36:13
99+
|
100+
LL | use crate::b;
101+
| ^^^^^^^^
102+
|
103+
help: consider adding a `#[cfg(test)]` to the parent module
104+
--> $DIR/unused-imports-in-test-module.rs:32:1
105+
|
106+
LL | mod a_test {
107+
| ^^^^^^^^^^
108+
109+
error: unused import: `super::a`
110+
--> $DIR/unused-imports-in-test-module.rs:41:9
111+
|
112+
LL | use super::a;
113+
| ^^^^^^^^
114+
|
115+
help: consider adding a `#[cfg(test)]` to the parent module
116+
--> $DIR/unused-imports-in-test-module.rs:40:1
117+
|
118+
LL | mod tests_a {
119+
| ^^^^^^^^^^^
120+
121+
error: unused import: `crate::b`
122+
--> $DIR/unused-imports-in-test-module.rs:44:13
123+
|
124+
LL | use crate::b;
125+
| ^^^^^^^^
126+
|
127+
help: consider adding a `#[cfg(test)]` to the parent module
128+
--> $DIR/unused-imports-in-test-module.rs:40:1
129+
|
130+
LL | mod tests_a {
131+
| ^^^^^^^^^^^
132+
133+
error: unused import: `super::a`
134+
--> $DIR/unused-imports-in-test-module.rs:49:9
135+
|
136+
LL | use super::a;
137+
| ^^^^^^^^
138+
|
139+
help: consider adding a `#[cfg(test)]` to the parent module
140+
--> $DIR/unused-imports-in-test-module.rs:48:1
141+
|
142+
LL | mod a_tests {
143+
| ^^^^^^^^^^^
144+
145+
error: unused import: `crate::b`
146+
--> $DIR/unused-imports-in-test-module.rs:52:13
147+
|
148+
LL | use crate::b;
149+
| ^^^^^^^^
150+
|
151+
help: consider adding a `#[cfg(test)]` to the parent module
152+
--> $DIR/unused-imports-in-test-module.rs:48:1
153+
|
154+
LL | mod a_tests {
155+
| ^^^^^^^^^^^
156+
157+
error: unused import: `super::a`
158+
--> $DIR/unused-imports-in-test-module.rs:57:9
159+
|
160+
LL | use super::a;
161+
| ^^^^^^^^
162+
163+
error: unused import: `crate::b`
164+
--> $DIR/unused-imports-in-test-module.rs:60:13
165+
|
166+
LL | use crate::b;
167+
| ^^^^^^^^
168+
169+
error: aborting due to 15 previous errors
62170

0 commit comments

Comments
 (0)