Skip to content

Commit 790950a

Browse files
authored
Rollup merge of #91770 - TaKO8Ki:suggest-adding-cfg-test, r=joshtriplett
Suggest adding a `#[cfg(test)]` to to a test module closes #88138
2 parents af09d24 + 6f8ad6d commit 790950a

File tree

6 files changed

+264
-4
lines changed

6 files changed

+264
-4
lines changed

compiler/rustc_lint/src/context.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,22 @@ pub trait LintContext: Sized {
661661
BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
662662
db.span_suggestion(span, &note, sugg, Applicability::MaybeIncorrect);
663663
}
664-
BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
664+
BuiltinLintDiagnostics::UnusedImports(message, replaces, in_test_module) => {
665665
if !replaces.is_empty() {
666666
db.tool_only_multipart_suggestion(
667667
&message,
668668
replaces,
669669
Applicability::MachineApplicable,
670670
);
671671
}
672+
673+
if let Some(span) = in_test_module {
674+
let def_span = self.sess().source_map().guess_head_span(span);
675+
db.span_help(
676+
span.shrink_to_lo().to(def_span),
677+
"consider adding a `#[cfg(test)]` to the containing module",
678+
);
679+
}
672680
}
673681
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
674682
for (span, is_imported) in spans {

compiler/rustc_lint_defs/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub enum BuiltinLintDiagnostics {
289289
ProcMacroDeriveResolutionFallback(Span),
290290
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
291291
UnknownCrateTypes(Span, String, String),
292-
UnusedImports(String, Vec<(Span, String)>),
292+
UnusedImports(String, Vec<(Span, String)>, Option<Span>),
293293
RedundantImport(Vec<(Span, bool)>, Ident),
294294
DeprecatedMacro(Option<Symbol>, Span),
295295
MissingAbi(Span, Abi),

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'a> Resolver<'a> {
108108
/// Reachable macros with block module parents exist due to `#[macro_export] macro_rules!`,
109109
/// but they cannot use def-site hygiene, so the assumption holds
110110
/// (<https://github.com/rust-lang/rust/pull/77984#issuecomment-712445508>).
111-
fn get_nearest_non_block_module(&mut self, mut def_id: DefId) -> Module<'a> {
111+
crate fn get_nearest_non_block_module(&mut self, mut def_id: DefId) -> Module<'a> {
112112
loop {
113113
match self.get_module(def_id) {
114114
Some(module) => return module,

compiler/rustc_resolve/src/check_unused.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// in the last step
2525

2626
use crate::imports::ImportKind;
27+
use crate::module_to_string;
2728
use crate::Resolver;
2829

2930
use rustc_ast as ast;
@@ -314,12 +315,29 @@ impl Resolver<'_> {
314315
"remove the unused import"
315316
};
316317

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)
331+
}
332+
_ => None,
333+
};
334+
317335
visitor.r.lint_buffer.buffer_lint_with_diagnostic(
318336
UNUSED_IMPORTS,
319337
unused.use_tree_id,
320338
ms,
321339
&msg,
322-
BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes),
340+
BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes, test_module_span),
323341
);
324342
}
325343
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![deny(unused_imports)]
2+
3+
use std::io::BufRead; //~ ERROR unused import: `std::io::BufRead`
4+
5+
fn a() {}
6+
fn b() {}
7+
8+
mod test {
9+
use super::a; //~ ERROR unused import: `super::a`
10+
11+
fn foo() {
12+
use crate::b; //~ ERROR unused import: `crate::b`
13+
}
14+
}
15+
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+
24+
mod test_a {
25+
use super::a; //~ ERROR unused import: `super::a`
26+
27+
fn foo() {
28+
use crate::b; //~ ERROR unused import: `crate::b`
29+
}
30+
}
31+
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+
64+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
error: unused import: `std::io::BufRead`
2+
--> $DIR/unused-imports-in-test-module.rs:3:5
3+
|
4+
LL | use std::io::BufRead;
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-imports-in-test-module.rs:1:9
9+
|
10+
LL | #![deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: unused import: `super::a`
14+
--> $DIR/unused-imports-in-test-module.rs:9:9
15+
|
16+
LL | use super::a;
17+
| ^^^^^^^^
18+
|
19+
help: consider adding a `#[cfg(test)]` to the containing module
20+
--> $DIR/unused-imports-in-test-module.rs:8:1
21+
|
22+
LL | mod test {
23+
| ^^^^^^^^
24+
25+
error: unused import: `crate::b`
26+
--> $DIR/unused-imports-in-test-module.rs:12:13
27+
|
28+
LL | use crate::b;
29+
| ^^^^^^^^
30+
|
31+
help: consider adding a `#[cfg(test)]` to the containing module
32+
--> $DIR/unused-imports-in-test-module.rs:8:1
33+
|
34+
LL | mod test {
35+
| ^^^^^^^^
36+
37+
error: unused import: `super::a`
38+
--> $DIR/unused-imports-in-test-module.rs:17:9
39+
|
40+
LL | use super::a;
41+
| ^^^^^^^^
42+
|
43+
help: consider adding a `#[cfg(test)]` to the containing module
44+
--> $DIR/unused-imports-in-test-module.rs:16:1
45+
|
46+
LL | mod tests {
47+
| ^^^^^^^^^
48+
49+
error: unused import: `crate::b`
50+
--> $DIR/unused-imports-in-test-module.rs:20:13
51+
|
52+
LL | use crate::b;
53+
| ^^^^^^^^
54+
|
55+
help: consider adding a `#[cfg(test)]` to the containing module
56+
--> $DIR/unused-imports-in-test-module.rs:16:1
57+
|
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 containing 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 containing module
80+
--> $DIR/unused-imports-in-test-module.rs:24:1
81+
|
82+
LL | mod test_a {
83+
| ^^^^^^^^^^
84+
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 containing 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 containing 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 containing 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 containing 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 containing 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 containing 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
170+

0 commit comments

Comments
 (0)