Take into account sub modules

This commit is contained in:
ThibsG 2021-03-28 09:35:44 +02:00
parent 1768efa333
commit 6985d13a97
4 changed files with 34 additions and 10 deletions

View File

@ -1,7 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::in_macro;
use if_chain::if_chain;
use rustc_ast::{Crate, Item, ItemKind, UseTreeKind};
use rustc_ast::{Crate, Item, ItemKind, ModKind, UseTreeKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
@ -83,13 +82,19 @@ impl EarlyLintPass for SingleComponentPathImports {
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;
if in_macro(item.span) || item.vis.kind.is_pub() {
return;
}
match &item.kind {
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
for item in items.iter() {
self.track_uses(&item);
}
},
ItemKind::Use(use_tree) => {
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 {
@ -117,7 +122,8 @@ impl SingleComponentPathImports {
}
}
}
}
},
_ => {},
}
}
}

View File

@ -19,3 +19,9 @@ fn main() {
// False positive #5154, shouldn't trigger lint.
m!();
}
mod hello_mod {
#[allow(dead_code)]
fn hello_mod() {}
}

View File

@ -19,3 +19,9 @@ fn main() {
// False positive #5154, shouldn't trigger lint.
m!();
}
mod hello_mod {
use regex;
#[allow(dead_code)]
fn hello_mod() {}
}

View File

@ -6,5 +6,11 @@ LL | use regex;
|
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
error: aborting due to previous error
error: this import is redundant
--> $DIR/single_component_path_imports.rs:24:5
|
LL | use regex;
| ^^^^^^^^^^ help: remove it entirely
error: aborting due to 2 previous errors