about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-12-21 08:33:39 +0100
committerGitHub <noreply@github.com>2021-12-21 08:33:39 +0100
commit790950a53039a7eaf6429f81e0ede13ebade46b7 (patch)
tree18f2a8708a03c55c42f7226c498ffe064e451413
parentaf09d24ff66b336c8bc76b66dce19c5a4507e881 (diff)
parent6f8ad6d83ab8cc52769c6abae2b869540fe38d04 (diff)
downloadrust-790950a53039a7eaf6429f81e0ede13ebade46b7.tar.gz
rust-790950a53039a7eaf6429f81e0ede13ebade46b7.zip
Rollup merge of #91770 - TaKO8Ki:suggest-adding-cfg-test, r=joshtriplett
Suggest adding a `#[cfg(test)]` to to a test module

closes #88138
-rw-r--r--compiler/rustc_lint/src/context.rs10
-rw-r--r--compiler/rustc_lint_defs/src/lib.rs2
-rw-r--r--compiler/rustc_resolve/src/build_reduced_graph.rs2
-rw-r--r--compiler/rustc_resolve/src/check_unused.rs20
-rw-r--r--src/test/ui/imports/unused-imports-in-test-module.rs64
-rw-r--r--src/test/ui/imports/unused-imports-in-test-module.stderr170
6 files changed, 264 insertions, 4 deletions
diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs
index f024b4bb74c..69c376c6169 100644
--- a/compiler/rustc_lint/src/context.rs
+++ b/compiler/rustc_lint/src/context.rs
@@ -661,7 +661,7 @@ pub trait LintContext: Sized {
                 BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
                     db.span_suggestion(span, &note, sugg, Applicability::MaybeIncorrect);
                 }
-                BuiltinLintDiagnostics::UnusedImports(message, replaces) => {
+                BuiltinLintDiagnostics::UnusedImports(message, replaces, in_test_module) => {
                     if !replaces.is_empty() {
                         db.tool_only_multipart_suggestion(
                             &message,
@@ -669,6 +669,14 @@ pub trait LintContext: Sized {
                             Applicability::MachineApplicable,
                         );
                     }
+
+                    if let Some(span) = in_test_module {
+                        let def_span = self.sess().source_map().guess_head_span(span);
+                        db.span_help(
+                            span.shrink_to_lo().to(def_span),
+                            "consider adding a `#[cfg(test)]` to the containing module",
+                        );
+                    }
                 }
                 BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
                     for (span, is_imported) in spans {
diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs
index e22c9c68de6..97f6df51f88 100644
--- a/compiler/rustc_lint_defs/src/lib.rs
+++ b/compiler/rustc_lint_defs/src/lib.rs
@@ -289,7 +289,7 @@ pub enum BuiltinLintDiagnostics {
     ProcMacroDeriveResolutionFallback(Span),
     MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
     UnknownCrateTypes(Span, String, String),
-    UnusedImports(String, Vec<(Span, String)>),
+    UnusedImports(String, Vec<(Span, String)>, Option<Span>),
     RedundantImport(Vec<(Span, bool)>, Ident),
     DeprecatedMacro(Option<Symbol>, Span),
     MissingAbi(Span, Abi),
diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs
index d45c064d5e3..74edc3a2d5e 100644
--- a/compiler/rustc_resolve/src/build_reduced_graph.rs
+++ b/compiler/rustc_resolve/src/build_reduced_graph.rs
@@ -108,7 +108,7 @@ impl<'a> Resolver<'a> {
     /// Reachable macros with block module parents exist due to `#[macro_export] macro_rules!`,
     /// but they cannot use def-site hygiene, so the assumption holds
     /// (<https://github.com/rust-lang/rust/pull/77984#issuecomment-712445508>).
-    fn get_nearest_non_block_module(&mut self, mut def_id: DefId) -> Module<'a> {
+    crate fn get_nearest_non_block_module(&mut self, mut def_id: DefId) -> Module<'a> {
         loop {
             match self.get_module(def_id) {
                 Some(module) => return module,
diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs
index 63699128e9e..601f2d96ff5 100644
--- a/compiler/rustc_resolve/src/check_unused.rs
+++ b/compiler/rustc_resolve/src/check_unused.rs
@@ -24,6 +24,7 @@
 //    in the last step
 
 use crate::imports::ImportKind;
+use crate::module_to_string;
 use crate::Resolver;
 
 use rustc_ast as ast;
@@ -314,12 +315,29 @@ 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)
+                }
+                _ => None,
+            };
+
             visitor.r.lint_buffer.buffer_lint_with_diagnostic(
                 UNUSED_IMPORTS,
                 unused.use_tree_id,
                 ms,
                 &msg,
-                BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes),
+                BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes, test_module_span),
             );
         }
     }
diff --git a/src/test/ui/imports/unused-imports-in-test-module.rs b/src/test/ui/imports/unused-imports-in-test-module.rs
new file mode 100644
index 00000000000..b003b99b6cf
--- /dev/null
+++ b/src/test/ui/imports/unused-imports-in-test-module.rs
@@ -0,0 +1,64 @@
+#![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`
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/imports/unused-imports-in-test-module.stderr b/src/test/ui/imports/unused-imports-in-test-module.stderr
new file mode 100644
index 00000000000..2efea5b3609
--- /dev/null
+++ b/src/test/ui/imports/unused-imports-in-test-module.stderr
@@ -0,0 +1,170 @@
+error: unused import: `std::io::BufRead`
+  --> $DIR/unused-imports-in-test-module.rs:3:5
+   |
+LL | use std::io::BufRead;
+   |     ^^^^^^^^^^^^^^^^
+   |
+note: the lint level is defined here
+  --> $DIR/unused-imports-in-test-module.rs:1:9
+   |
+LL | #![deny(unused_imports)]
+   |         ^^^^^^^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:9:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:8:1
+   |
+LL | mod test {
+   | ^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:12:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:8:1
+   |
+LL | mod test {
+   | ^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:17:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:16:1
+   |
+LL | mod tests {
+   | ^^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:20:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:16:1
+   |
+LL | mod tests {
+   | ^^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:25:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:24:1
+   |
+LL | mod test_a {
+   | ^^^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:28:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:24:1
+   |
+LL | mod test_a {
+   | ^^^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:33:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:32:1
+   |
+LL | mod a_test {
+   | ^^^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:36:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:32:1
+   |
+LL | mod a_test {
+   | ^^^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:41:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:40:1
+   |
+LL | mod tests_a {
+   | ^^^^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:44:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:40:1
+   |
+LL | mod tests_a {
+   | ^^^^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:49:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:48:1
+   |
+LL | mod a_tests {
+   | ^^^^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:52:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+   |
+help: consider adding a `#[cfg(test)]` to the containing module
+  --> $DIR/unused-imports-in-test-module.rs:48:1
+   |
+LL | mod a_tests {
+   | ^^^^^^^^^^^
+
+error: unused import: `super::a`
+  --> $DIR/unused-imports-in-test-module.rs:57:9
+   |
+LL |     use super::a;
+   |         ^^^^^^^^
+
+error: unused import: `crate::b`
+  --> $DIR/unused-imports-in-test-module.rs:60:13
+   |
+LL |         use crate::b;
+   |             ^^^^^^^^
+
+error: aborting due to 15 previous errors
+