Skip to content

Commit 67fad01

Browse files
committed
Auto merge of rust-lang#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 rust-lang#5210 changelog: none
2 parents 75e20ba + 297e84f commit 67fad01

8 files changed

+219
-19
lines changed

clippy_lints/src/single_component_path_imports.rs

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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;
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,120 @@ 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 {
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+
let can_suggest = single_use.2;
70+
if can_suggest {
5171
span_lint_and_sugg(
5272
cx,
5373
SINGLE_COMPONENT_PATH_IMPORTS,
54-
item.span,
74+
single_use.1,
5575
"this import is redundant",
5676
"remove it entirely",
5777
String::new(),
58-
Applicability::MachineApplicable
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",
5988
);
6089
}
6190
}
6291
}
6392
}
93+
94+
fn track_uses(
95+
cx: &EarlyContext<'_>,
96+
item: &Item,
97+
imports_reused_with_self: &mut Vec<Symbol>,
98+
single_use_usages: &mut Vec<(Symbol, Span, bool)>,
99+
) {
100+
if in_macro(item.span) || item.vis.kind.is_pub() {
101+
return;
102+
}
103+
104+
match &item.kind {
105+
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
106+
check_mod(cx, &items);
107+
},
108+
ItemKind::Use(use_tree) => {
109+
let segments = &use_tree.prefix.segments;
110+
111+
// keep track of `use some_module;` usages
112+
if segments.len() == 1 {
113+
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
114+
let ident = &segments[0].ident;
115+
single_use_usages.push((ident.name, item.span, true));
116+
}
117+
return;
118+
}
119+
120+
if segments.is_empty() {
121+
// keep track of `use {some_module, some_other_module};` usages
122+
if let UseTreeKind::Nested(trees) = &use_tree.kind {
123+
for tree in trees {
124+
let segments = &tree.0.prefix.segments;
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+
}
149+
}
150+
}
151+
}
152+
}
153+
},
154+
_ => {},
155+
}
156+
}

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: 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+
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)