about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibsG <thibsg@pm.me>2021-03-14 19:35:35 +0100
committerThibsG <thibsg@pm.me>2021-03-28 09:37:05 +0200
commit1768efa33344022c550f3cfcd30ede168bcf3143 (patch)
tree16a3048c9c0d86f4f29558966e28abc7a9f0013f
parent8e56a2b27f005e7f81756ccc281eca69900dddd7 (diff)
downloadrust-1768efa33344022c550f3cfcd30ede168bcf3143.tar.gz
rust-1768efa33344022c550f3cfcd30ede168bcf3143.zip
Fix FP in `single_component_path_imports` lint
-rw-r--r--clippy_lints/src/lib.rs2
-rw-r--r--clippy_lints/src/single_component_path_imports.rs90
-rw-r--r--tests/ui/single_component_path_imports_self_after.rs16
-rw-r--r--tests/ui/single_component_path_imports_self_before.rs17
4 files changed, 109 insertions, 16 deletions
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index f013613119c..97557fefe3e 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -1232,7 +1232,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
     store.register_early_pass(|| box as_conversions::AsConversions);
     store.register_late_pass(|| box let_underscore::LetUnderscore);
     store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
-    store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports);
+    store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports::default());
     let max_fn_params_bools = conf.max_fn_params_bools;
     let max_struct_bools = conf.max_struct_bools;
     store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
diff --git a/clippy_lints/src/single_component_path_imports.rs b/clippy_lints/src/single_component_path_imports.rs
index c9d72aabb6a..ee74ef97bcd 100644
--- a/clippy_lints/src/single_component_path_imports.rs
+++ b/clippy_lints/src/single_component_path_imports.rs
@@ -1,11 +1,13 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::in_macro;
 use if_chain::if_chain;
-use rustc_ast::{Item, ItemKind, UseTreeKind};
+use rustc_ast::{Crate, Item, ItemKind, UseTreeKind};
 use rustc_errors::Applicability;
 use rustc_lint::{EarlyContext, EarlyLintPass};
-use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_session::{declare_tool_lint, impl_lint_pass};
 use rustc_span::edition::Edition;
+use rustc_span::symbol::kw;
+use rustc_span::{Span, Symbol};
 
 declare_clippy_lint! {
     /// **What it does:** Checking for imports with single component use path.
@@ -35,29 +37,87 @@ declare_clippy_lint! {
     "imports with single component path are redundant"
 }
 
-declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
+#[derive(Default)]
+pub struct SingleComponentPathImports {
+    /// keep track of imports reused with `self` keyword,
+    /// such as `self::crypto_hash` in the example below
+    ///
+    /// ```rust,ignore
+    /// use self::crypto_hash::{Algorithm, Hasher};
+    /// ```
+    imports_reused_with_self: Vec<Symbol>,
+    /// keep track of single use statements
+    /// such as `crypto_hash` in the example below
+    ///
+    /// ```rust,ignore
+    /// use crypto_hash;
+    /// ```
+    single_use_usages: Vec<(Symbol, Span)>,
+}
+
+impl_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
 
 impl EarlyLintPass for SingleComponentPathImports {
-    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
-        if_chain! {
-            if !in_macro(item.span);
-            if cx.sess.opts.edition >= Edition::Edition2018;
-            if !item.vis.kind.is_pub();
-            if let ItemKind::Use(use_tree) = &item.kind;
-            if let segments = &use_tree.prefix.segments;
-            if segments.len() == 1;
-            if let UseTreeKind::Simple(None, _, _) = use_tree.kind;
-            then {
+    fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
+        if cx.sess.opts.edition < Edition::Edition2018 {
+            return;
+        }
+        for item in &krate.items {
+            self.track_uses(&item);
+        }
+        for single_use in &self.single_use_usages {
+            if !self.imports_reused_with_self.contains(&single_use.0) {
                 span_lint_and_sugg(
                     cx,
                     SINGLE_COMPONENT_PATH_IMPORTS,
-                    item.span,
+                    single_use.1,
                     "this import is redundant",
                     "remove it entirely",
                     String::new(),
-                    Applicability::MachineApplicable
+                    Applicability::MachineApplicable,
                 );
             }
         }
     }
 }
+
+impl SingleComponentPathImports {
+    fn track_uses(&mut self, item: &Item) {
+        if_chain! {
+            if !in_macro(item.span);
+            if !item.vis.kind.is_pub();
+            if let ItemKind::Use(use_tree) = &item.kind;
+            if let segments = &use_tree.prefix.segments;
+
+            then {
+                // keep track of `use some_module;` usages
+                if segments.len() == 1 {
+                    if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
+                        let ident = &segments[0].ident;
+                        self.single_use_usages.push((ident.name, item.span));
+                    }
+                    return;
+                }
+
+                // keep track of `use self::some_module` usages
+                if segments[0].ident.name == kw::SelfLower {
+                    // simple case such as `use self::module::SomeStruct`
+                    if segments.len() > 1 {
+                        self.imports_reused_with_self.push(segments[1].ident.name);
+                        return;
+                    }
+
+                    // nested case such as `use self::{module1::Struct1, module2::Struct2}`
+                    if let UseTreeKind::Nested(trees) = &use_tree.kind {
+                        for tree in trees {
+                            let segments = &tree.0.prefix.segments;
+                            if !segments.is_empty() {
+                                self.imports_reused_with_self.push(segments[0].ident.name);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
diff --git a/tests/ui/single_component_path_imports_self_after.rs b/tests/ui/single_component_path_imports_self_after.rs
new file mode 100644
index 00000000000..94319ade0ac
--- /dev/null
+++ b/tests/ui/single_component_path_imports_self_after.rs
@@ -0,0 +1,16 @@
+// edition:2018
+#![warn(clippy::single_component_path_imports)]
+#![allow(unused_imports)]
+
+use self::regex::{Regex as xeger, RegexSet as tesxeger};
+pub use self::{
+    regex::{Regex, RegexSet},
+    some_mod::SomeType,
+};
+use regex;
+
+mod some_mod {
+    pub struct SomeType;
+}
+
+fn main() {}
diff --git a/tests/ui/single_component_path_imports_self_before.rs b/tests/ui/single_component_path_imports_self_before.rs
new file mode 100644
index 00000000000..c7437b23456
--- /dev/null
+++ b/tests/ui/single_component_path_imports_self_before.rs
@@ -0,0 +1,17 @@
+// edition:2018
+#![warn(clippy::single_component_path_imports)]
+#![allow(unused_imports)]
+
+use regex;
+
+use self::regex::{Regex as xeger, RegexSet as tesxeger};
+pub use self::{
+    regex::{Regex, RegexSet},
+    some_mod::SomeType,
+};
+
+mod some_mod {
+    pub struct SomeType;
+}
+
+fn main() {}