about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-03-14 23:39:50 +0000
committerbors <bors@rust-lang.org>2022-03-14 23:39:50 +0000
commit9a1f6a9795d41f787b099e7cfcb8dcdd5e53584c (patch)
tree77312192480b0d6205b8e0a0df00ee011ecd0716
parent0e1311b70f0b11cae18b0f7703ceed34b19eef37 (diff)
parent2ee53723897407ea31a1803af9bbedf98a49f934 (diff)
downloadrust-9a1f6a9795d41f787b099e7cfcb8dcdd5e53584c.tar.gz
rust-9a1f6a9795d41f787b099e7cfcb8dcdd5e53584c.zip
Auto merge of #8537 - xFrednet:7923-single-component-path-imports-for-macros, r=llogiq
Allow `single_component_path_imports` for all macros

Closes: https://github.com/rust-lang/rust-clippy/issues/7923

It can be useful to have `use macro_name` regardless of the visibility. This removes the visibility filter.

changelog: [`single_component_path_imports`]: no longer triggers on macros
-rw-r--r--clippy_lints/src/single_component_path_imports.rs18
-rw-r--r--tests/ui/single_component_path_imports_macro.fixed20
-rw-r--r--tests/ui/single_component_path_imports_macro.rs4
-rw-r--r--tests/ui/single_component_path_imports_macro.stderr10
4 files changed, 9 insertions, 43 deletions
diff --git a/clippy_lints/src/single_component_path_imports.rs b/clippy_lints/src/single_component_path_imports.rs
index 961cdb317e7..66b79513032 100644
--- a/clippy_lints/src/single_component_path_imports.rs
+++ b/clippy_lints/src/single_component_path_imports.rs
@@ -1,5 +1,5 @@
 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
-use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind, VisibilityKind};
+use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind};
 use rustc_errors::Applicability;
 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -76,14 +76,13 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
         );
     }
 
-    for single_use in &single_use_usages {
-        if !imports_reused_with_self.contains(&single_use.0) {
-            let can_suggest = single_use.2;
+    for (name, span, can_suggest) in single_use_usages {
+        if !imports_reused_with_self.contains(&name) {
             if can_suggest {
                 span_lint_and_sugg(
                     cx,
                     SINGLE_COMPONENT_PATH_IMPORTS,
-                    single_use.1,
+                    span,
                     "this import is redundant",
                     "remove it entirely",
                     String::new(),
@@ -93,7 +92,7 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
                 span_lint_and_help(
                     cx,
                     SINGLE_COMPONENT_PATH_IMPORTS,
-                    single_use.1,
+                    span,
                     "this import is redundant",
                     None,
                     "remove this import",
@@ -124,14 +123,11 @@ fn track_uses(
         ItemKind::Use(use_tree) => {
             let segments = &use_tree.prefix.segments;
 
-            let should_report =
-                |name: &Symbol| !macros.contains(name) || matches!(item.vis.kind, VisibilityKind::Inherited);
-
             // keep track of `use some_module;` usages
             if segments.len() == 1 {
                 if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
                     let name = segments[0].ident.name;
-                    if should_report(&name) {
+                    if !macros.contains(&name) {
                         single_use_usages.push((name, item.span, true));
                     }
                 }
@@ -146,7 +142,7 @@ fn track_uses(
                         if segments.len() == 1 {
                             if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
                                 let name = segments[0].ident.name;
-                                if should_report(&name) {
+                                if !macros.contains(&name) {
                                     single_use_usages.push((name, tree.0.span, false));
                                 }
                             }
diff --git a/tests/ui/single_component_path_imports_macro.fixed b/tests/ui/single_component_path_imports_macro.fixed
deleted file mode 100644
index e43f5d381aa..00000000000
--- a/tests/ui/single_component_path_imports_macro.fixed
+++ /dev/null
@@ -1,20 +0,0 @@
-// run-rustfix
-#![warn(clippy::single_component_path_imports)]
-#![allow(unused_imports)]
-
-// #7106: use statements exporting a macro within a crate should not trigger lint
-
-macro_rules! m1 {
-    () => {};
-}
-pub(crate) use m1; // ok
-
-macro_rules! m2 {
-    () => {};
-}
- // fail
-
-fn main() {
-    m1!();
-    m2!();
-}
diff --git a/tests/ui/single_component_path_imports_macro.rs b/tests/ui/single_component_path_imports_macro.rs
index 3c65ca3054c..fda294a6154 100644
--- a/tests/ui/single_component_path_imports_macro.rs
+++ b/tests/ui/single_component_path_imports_macro.rs
@@ -1,8 +1,8 @@
-// run-rustfix
 #![warn(clippy::single_component_path_imports)]
 #![allow(unused_imports)]
 
 // #7106: use statements exporting a macro within a crate should not trigger lint
+// #7923: normal `use` statements of macros should also not trigger the lint
 
 macro_rules! m1 {
     () => {};
@@ -12,7 +12,7 @@ pub(crate) use m1; // ok
 macro_rules! m2 {
     () => {};
 }
-use m2; // fail
+use m2; // ok
 
 fn main() {
     m1!();
diff --git a/tests/ui/single_component_path_imports_macro.stderr b/tests/ui/single_component_path_imports_macro.stderr
deleted file mode 100644
index 37d5176129f..00000000000
--- a/tests/ui/single_component_path_imports_macro.stderr
+++ /dev/null
@@ -1,10 +0,0 @@
-error: this import is redundant
-  --> $DIR/single_component_path_imports_macro.rs:15:1
-   |
-LL | use m2; // fail
-   | ^^^^^^^ help: remove it entirely
-   |
-   = note: `-D clippy::single-component-path-imports` implied by `-D warnings`
-
-error: aborting due to previous error
-