Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6985d13

Browse files
committed
Take into account sub modules
1 parent 1768efa commit 6985d13

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

clippy_lints/src/single_component_path_imports.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::in_macro;
3-
use if_chain::if_chain;
4-
use rustc_ast::{Crate, Item, ItemKind, UseTreeKind};
3+
use rustc_ast::{Crate, Item, ItemKind, ModKind, UseTreeKind};
54
use rustc_errors::Applicability;
65
use rustc_lint::{EarlyContext, EarlyLintPass};
76
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -83,13 +82,19 @@ impl EarlyLintPass for SingleComponentPathImports {
8382

8483
impl SingleComponentPathImports {
8584
fn track_uses(&mut self, item: &Item) {
86-
if_chain! {
87-
if !in_macro(item.span);
88-
if !item.vis.kind.is_pub();
89-
if let ItemKind::Use(use_tree) = &item.kind;
90-
if let segments = &use_tree.prefix.segments;
85+
if in_macro(item.span) || item.vis.kind.is_pub() {
86+
return;
87+
}
88+
89+
match &item.kind {
90+
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
91+
for item in items.iter() {
92+
self.track_uses(&item);
93+
}
94+
},
95+
ItemKind::Use(use_tree) => {
96+
let segments = &use_tree.prefix.segments;
9197

92-
then {
9398
// keep track of `use some_module;` usages
9499
if segments.len() == 1 {
95100
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
@@ -117,7 +122,8 @@ impl SingleComponentPathImports {
117122
}
118123
}
119124
}
120-
}
125+
},
126+
_ => {},
121127
}
122128
}
123129
}

tests/ui/single_component_path_imports.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ fn main() {
1919
// False positive #5154, shouldn't trigger lint.
2020
m!();
2121
}
22+
23+
mod hello_mod {
24+
25+
#[allow(dead_code)]
26+
fn hello_mod() {}
27+
}

tests/ui/single_component_path_imports.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ fn main() {
1919
// False positive #5154, shouldn't trigger lint.
2020
m!();
2121
}
22+
23+
mod hello_mod {
24+
use regex;
25+
#[allow(dead_code)]
26+
fn hello_mod() {}
27+
}

tests/ui/single_component_path_imports.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ LL | use regex;
66
|
77
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: this import is redundant
10+
--> $DIR/single_component_path_imports.rs:24:5
11+
|
12+
LL | use regex;
13+
| ^^^^^^^^^^ help: remove it entirely
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)