Skip to content

Commit 067cc44

Browse files
committed
Auto merge of #6905 - ThibsG:fpSingleComponentPathImports5210, r=giraffate
Fix FP in `single_component_path_imports` lint Fix FP in `single_component_path_imports` lint when the import is reused with `self`, like in `use self::module`. Fixes #5210 changelog: none
2 parents 81f9946 + 81dfb9e commit 067cc44

6 files changed

+157
-26
lines changed

clippy_lints/src/single_component_path_imports.rs

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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::{Item, ItemKind, UseTreeKind};
3+
use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, UseTreeKind};
54
use rustc_errors::Applicability;
65
use rustc_lint::{EarlyContext, EarlyLintPass};
76
use rustc_session::{declare_lint_pass, declare_tool_lint};
8-
use rustc_span::edition::Edition;
7+
use rustc_span::{edition::Edition, symbol::kw, Span, Symbol};
98

109
declare_clippy_lint! {
1110
/// **What it does:** Checking for imports with single component use path.
@@ -38,26 +37,93 @@ declare_clippy_lint! {
3837
declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
3938

4039
impl EarlyLintPass for SingleComponentPathImports {
41-
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
42-
if_chain! {
43-
if !in_macro(item.span);
44-
if cx.sess.opts.edition >= Edition::Edition2018;
45-
if !item.vis.kind.is_pub();
46-
if let ItemKind::Use(use_tree) = &item.kind;
47-
if let segments = &use_tree.prefix.segments;
48-
if segments.len() == 1;
49-
if let UseTreeKind::Simple(None, _, _) = use_tree.kind;
50-
then {
51-
span_lint_and_sugg(
52-
cx,
53-
SINGLE_COMPONENT_PATH_IMPORTS,
54-
item.span,
55-
"this import is redundant",
56-
"remove it entirely",
57-
String::new(),
58-
Applicability::MachineApplicable
59-
);
60-
}
40+
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
41+
if cx.sess.opts.edition < Edition::Edition2018 {
42+
return;
43+
}
44+
check_mod(cx, &krate.items);
45+
}
46+
}
47+
48+
fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
49+
// keep track of imports reused with `self` keyword,
50+
// such as `self::crypto_hash` in the example below
51+
// ```rust,ignore
52+
// use self::crypto_hash::{Algorithm, Hasher};
53+
// ```
54+
let mut imports_reused_with_self = Vec::new();
55+
56+
// keep track of single use statements
57+
// such as `crypto_hash` in the example below
58+
// ```rust,ignore
59+
// use crypto_hash;
60+
// ```
61+
let mut single_use_usages = Vec::new();
62+
63+
for item in items {
64+
track_uses(cx, &item, &mut imports_reused_with_self, &mut single_use_usages);
65+
}
66+
67+
for single_use in &single_use_usages {
68+
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+
);
6178
}
6279
}
6380
}
81+
82+
fn track_uses(
83+
cx: &EarlyContext<'_>,
84+
item: &Item,
85+
imports_reused_with_self: &mut Vec<Symbol>,
86+
single_use_usages: &mut Vec<(Symbol, Span)>,
87+
) {
88+
if in_macro(item.span) || item.vis.kind.is_pub() {
89+
return;
90+
}
91+
92+
match &item.kind {
93+
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
94+
check_mod(cx, &items);
95+
},
96+
ItemKind::Use(use_tree) => {
97+
let segments = &use_tree.prefix.segments;
98+
99+
// keep track of `use some_module;` usages
100+
if segments.len() == 1 {
101+
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
102+
let ident = &segments[0].ident;
103+
single_use_usages.push((ident.name, item.span));
104+
}
105+
return;
106+
}
107+
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}`
117+
if let UseTreeKind::Nested(trees) = &use_tree.kind {
118+
for tree in trees {
119+
let segments = &tree.0.prefix.segments;
120+
if !segments.is_empty() {
121+
imports_reused_with_self.push(segments[0].ident.name);
122+
}
123+
}
124+
}
125+
}
126+
},
127+
_ => {},
128+
}
129+
}

tests/ui/single_component_path_imports.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ 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+
}
28+
29+
mod hi_mod {
30+
use self::regex::{Regex, RegexSet};
31+
use regex;
32+
#[allow(dead_code)]
33+
fn hi_mod() {}
34+
}

tests/ui/single_component_path_imports.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ 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+
}
28+
29+
mod hi_mod {
30+
use self::regex::{Regex, RegexSet};
31+
use regex;
32+
#[allow(dead_code)]
33+
fn hi_mod() {}
34+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
error: this import is redundant
2+
--> $DIR/single_component_path_imports.rs:24:5
3+
|
4+
LL | use regex;
5+
| ^^^^^^^^^^ help: remove it entirely
6+
|
7+
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
8+
19
error: this import is redundant
210
--> $DIR/single_component_path_imports.rs:6:1
311
|
412
LL | use regex;
513
| ^^^^^^^^^^ help: remove it entirely
6-
|
7-
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
814

9-
error: aborting due to previous error
15+
error: aborting due to 2 previous errors
1016

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// edition:2018
2+
#![warn(clippy::single_component_path_imports)]
3+
#![allow(unused_imports)]
4+
5+
use self::regex::{Regex as xeger, RegexSet as tesxeger};
6+
pub use self::{
7+
regex::{Regex, RegexSet},
8+
some_mod::SomeType,
9+
};
10+
use regex;
11+
12+
mod some_mod {
13+
pub struct SomeType;
14+
}
15+
16+
fn main() {}
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+
7+
use self::regex::{Regex as xeger, RegexSet as tesxeger};
8+
pub use self::{
9+
regex::{Regex, RegexSet},
10+
some_mod::SomeType,
11+
};
12+
13+
mod some_mod {
14+
pub struct SomeType;
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)