Skip to content

Commit 297e84f

Browse files
committed
Handle imports which are nested directly
1 parent 81dfb9e commit 297e84f

3 files changed

+92
-23
lines changed

clippy_lints/src/single_component_path_imports.rs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::in_macro;
33
use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, UseTreeKind};
44
use rustc_errors::Applicability;
@@ -66,15 +66,27 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
6666

6767
for single_use in &single_use_usages {
6868
if !imports_reused_with_self.contains(&single_use.0) {
69-
span_lint_and_sugg(
70-
cx,
71-
SINGLE_COMPONENT_PATH_IMPORTS,
72-
single_use.1,
73-
"this import is redundant",
74-
"remove it entirely",
75-
String::new(),
76-
Applicability::MachineApplicable,
77-
);
69+
let can_suggest = single_use.2;
70+
if can_suggest {
71+
span_lint_and_sugg(
72+
cx,
73+
SINGLE_COMPONENT_PATH_IMPORTS,
74+
single_use.1,
75+
"this import is redundant",
76+
"remove it entirely",
77+
String::new(),
78+
Applicability::MachineApplicable,
79+
);
80+
} else {
81+
span_lint_and_help(
82+
cx,
83+
SINGLE_COMPONENT_PATH_IMPORTS,
84+
single_use.1,
85+
"this import is redundant",
86+
None,
87+
"remove this import",
88+
);
89+
}
7890
}
7991
}
8092
}
@@ -83,7 +95,7 @@ fn track_uses(
8395
cx: &EarlyContext<'_>,
8496
item: &Item,
8597
imports_reused_with_self: &mut Vec<Symbol>,
86-
single_use_usages: &mut Vec<(Symbol, Span)>,
98+
single_use_usages: &mut Vec<(Symbol, Span, bool)>,
8799
) {
88100
if in_macro(item.span) || item.vis.kind.is_pub() {
89101
return;
@@ -100,25 +112,40 @@ fn track_uses(
100112
if segments.len() == 1 {
101113
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
102114
let ident = &segments[0].ident;
103-
single_use_usages.push((ident.name, item.span));
115+
single_use_usages.push((ident.name, item.span, true));
104116
}
105117
return;
106118
}
107119

108-
// keep track of `use self::some_module` usages
109-
if segments[0].ident.name == kw::SelfLower {
110-
// simple case such as `use self::module::SomeStruct`
111-
if segments.len() > 1 {
112-
imports_reused_with_self.push(segments[1].ident.name);
113-
return;
114-
}
115-
116-
// nested case such as `use self::{module1::Struct1, module2::Struct2}`
120+
if segments.is_empty() {
121+
// keep track of `use {some_module, some_other_module};` usages
117122
if let UseTreeKind::Nested(trees) = &use_tree.kind {
118123
for tree in trees {
119124
let segments = &tree.0.prefix.segments;
120-
if !segments.is_empty() {
121-
imports_reused_with_self.push(segments[0].ident.name);
125+
if segments.len() == 1 {
126+
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
127+
let ident = &segments[0].ident;
128+
single_use_usages.push((ident.name, tree.0.span, false));
129+
}
130+
}
131+
}
132+
}
133+
} else {
134+
// keep track of `use self::some_module` usages
135+
if segments[0].ident.name == kw::SelfLower {
136+
// simple case such as `use self::module::SomeStruct`
137+
if segments.len() > 1 {
138+
imports_reused_with_self.push(segments[1].ident.name);
139+
return;
140+
}
141+
142+
// nested case such as `use self::{module1::Struct1, module2::Struct2}`
143+
if let UseTreeKind::Nested(trees) = &use_tree.kind {
144+
for tree in trees {
145+
let segments = &tree.0.prefix.segments;
146+
if !segments.is_empty() {
147+
imports_reused_with_self.push(segments[0].ident.name);
148+
}
122149
}
123150
}
124151
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2018
2+
#![warn(clippy::single_component_path_imports)]
3+
#![allow(unused_imports)]
4+
5+
use regex;
6+
use serde as edres;
7+
pub use serde;
8+
9+
fn main() {
10+
regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
11+
}
12+
13+
mod root_nested_use_mod {
14+
use {regex, serde};
15+
#[allow(dead_code)]
16+
fn root_nested_use_mod() {}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: this import is redundant
2+
--> $DIR/single_component_path_imports_nested_first.rs:14:10
3+
|
4+
LL | use {regex, serde};
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
8+
= help: remove this import
9+
10+
error: this import is redundant
11+
--> $DIR/single_component_path_imports_nested_first.rs:14:17
12+
|
13+
LL | use {regex, serde};
14+
| ^^^^^
15+
|
16+
= help: remove this import
17+
18+
error: this import is redundant
19+
--> $DIR/single_component_path_imports_nested_first.rs:5:1
20+
|
21+
LL | use regex;
22+
| ^^^^^^^^^^ help: remove it entirely
23+
24+
error: aborting due to 3 previous errors
25+

0 commit comments

Comments
 (0)