Take into account sub modules
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::in_macro;
|
use clippy_utils::in_macro;
|
||||||
use if_chain::if_chain;
|
use rustc_ast::{Crate, Item, ItemKind, ModKind, UseTreeKind};
|
||||||
use rustc_ast::{Crate, Item, ItemKind, UseTreeKind};
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||||
@@ -83,13 +82,19 @@ impl EarlyLintPass for SingleComponentPathImports {
|
|||||||
|
|
||||||
impl SingleComponentPathImports {
|
impl SingleComponentPathImports {
|
||||||
fn track_uses(&mut self, item: &Item) {
|
fn track_uses(&mut self, item: &Item) {
|
||||||
if_chain! {
|
if in_macro(item.span) || item.vis.kind.is_pub() {
|
||||||
if !in_macro(item.span);
|
return;
|
||||||
if !item.vis.kind.is_pub();
|
}
|
||||||
if let ItemKind::Use(use_tree) = &item.kind;
|
|
||||||
if let segments = &use_tree.prefix.segments;
|
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
|
// keep track of `use some_module;` usages
|
||||||
if segments.len() == 1 {
|
if segments.len() == 1 {
|
||||||
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
|
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
|
||||||
@@ -117,7 +122,8 @@ impl SingleComponentPathImports {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,3 +19,9 @@ fn main() {
|
|||||||
// False positive #5154, shouldn't trigger lint.
|
// False positive #5154, shouldn't trigger lint.
|
||||||
m!();
|
m!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod hello_mod {
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn hello_mod() {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,3 +19,9 @@ fn main() {
|
|||||||
// False positive #5154, shouldn't trigger lint.
|
// False positive #5154, shouldn't trigger lint.
|
||||||
m!();
|
m!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod hello_mod {
|
||||||
|
use regex;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn hello_mod() {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,5 +6,11 @@ LL | use regex;
|
|||||||
|
|
|
|
||||||
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
|
= 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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user