about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2021-10-23 18:55:02 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2021-12-16 11:48:58 +0900
commit6f8ad6d83ab8cc52769c6abae2b869540fe38d04 (patch)
treefd3ecc5c63fc38558a5589456afbdd1dc54ccd76 /compiler/rustc_resolve/src
parent3ee016ae4d4c6ee4a34faa2eb7fdae2ffa7c9b46 (diff)
downloadrust-6f8ad6d83ab8cc52769c6abae2b869540fe38d04.tar.gz
rust-6f8ad6d83ab8cc52769c6abae2b869540fe38d04.zip
suggest adding a `#[cfg(test)]` to test modules
remove a empty line

import `module_to_string`

use `contains("test")`

show a suggestion in case module starts_with/ends_with "test"

replace `parent` with `containing`
Diffstat (limited to 'compiler/rustc_resolve/src')
-rw-r--r--compiler/rustc_resolve/src/build_reduced_graph.rs2
-rw-r--r--compiler/rustc_resolve/src/check_unused.rs20
2 files changed, 20 insertions, 2 deletions
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),
             );
         }
     }