Skip to content

Commit e597832

Browse files
committed
apply changes from review
1 parent b9cf008 commit e597832

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

clippy_lints/src/unnecessary_self_imports.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::snippet;
32
use if_chain::if_chain;
4-
use rustc_ast::{Item, ItemKind, Path, PathSegment, UseTree, UseTreeKind};
3+
use rustc_ast::{Item, ItemKind, PathSegment, UseTree, 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::symbol::kw::SelfLower;
7+
use rustc_span::symbol::kw;
98

109
declare_clippy_lint! {
11-
/// **What it does:** Checks for imports ending in `::{self};`.
10+
/// **What it does:** Checks for imports ending in `::{self}`.
1211
///
13-
/// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `self`.
12+
/// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `::{self}`.
1413
///
15-
/// **Known problems:** Sometimes removing `::{self}` will break code (https://github.com/rust-lang/rustfmt/issues/3568).
14+
/// **Known problems:** Removing `::{self}` will cause any non-module items at the same path to also be imported.
15+
/// This might cause a naming conflict (https://github.com/rust-lang/rustfmt/issues/3568). This lint makes no attempt
16+
/// to detect this scenario and that is why it is a restriction lint.
1617
///
1718
/// **Example:**
1819
///
@@ -25,45 +26,40 @@ declare_clippy_lint! {
2526
/// ```
2627
pub UNNECESSARY_SELF_IMPORTS,
2728
restriction,
28-
"imports ending in `self`, which can be omitted"
29+
"imports ending in `::{self}`, which can be omitted"
2930
}
3031

3132
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
3233

3334
impl EarlyLintPass for UnnecessarySelfImports {
3435
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
3536
if_chain! {
36-
if let ItemKind::Use(
37-
UseTree { kind: UseTreeKind::Nested(nodes), prefix: Path { span, .. }, .. }
38-
) = &item.kind;
39-
if nodes.len() == 1;
40-
if let (UseTree { prefix, kind, .. }, _) = &nodes[0];
41-
if let PathSegment { ident, .. } = prefix.segments[0];
42-
if ident.name == SelfLower;
37+
if let ItemKind::Use(use_tree) = &item.kind;
38+
if let UseTree { kind: UseTreeKind::Nested(nodes), .. } = use_tree;
39+
if let [(UseTree { prefix, kind, .. }, _)] = &**nodes;
40+
if let [PathSegment { ident, .. }, ..] = &*prefix.segments;
41+
if ident.name == kw::SelfLower;
42+
if let Some(last_segment) = use_tree.prefix.segments.last();
4343

4444
then {
45-
let adjusted_span = item.span.with_hi(span.hi());
46-
let snippet = if let UseTreeKind::Simple(Some(alias), ..) = kind {
47-
format!(
48-
"{} as {};",
49-
snippet(cx, adjusted_span, ".."),
50-
snippet(cx, alias.span, "..")
51-
)
52-
} else {
53-
format!(
54-
"{};",
55-
snippet(cx, adjusted_span, "..")
56-
)
57-
};
5845
span_lint_and_then(
5946
cx,
6047
UNNECESSARY_SELF_IMPORTS,
6148
item.span,
62-
"import ending with `self`",
49+
"import ending with `::{self}`",
6350
|diag| {
64-
diag.span_suggestion(item.span, "consider omitting `self`", snippet, Applicability::MaybeIncorrect);
51+
diag.span_suggestion(
52+
last_segment.span().with_hi(item.span.hi()),
53+
"consider omitting `::{self}`",
54+
format!(
55+
"{}{};",
56+
last_segment.ident,
57+
if let UseTreeKind::Simple(Some(alias), ..) = kind { format!(" as {}", alias) } else { String::new() },
58+
),
59+
Applicability::MaybeIncorrect,
60+
);
6561
diag.note("this will slightly change semantics; any non-module items at the same path will also be imported");
66-
}
62+
},
6763
);
6864
}
6965
}

tests/ui/unnecessary_self_imports.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::collections::hash_map::{self, *};
66
use std::fs as alias;
7-
use std::io;
7+
use std::io::{self, Read};
8+
use std::rc;
89

910
fn main() {}

tests/ui/unnecessary_self_imports.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::collections::hash_map::{self, *};
66
use std::fs::{self as alias};
7-
use std::io::{self};
7+
use std::io::{self, Read};
8+
use std::rc::{self};
89

910
fn main() {}

tests/ui/unnecessary_self_imports.stderr

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
error: import ending with `self`
1+
error: import ending with `::{self}`
22
--> $DIR/unnecessary_self_imports.rs:6:1
33
|
44
LL | use std::fs::{self as alias};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider omitting `self`: `use std::fs as alias;`
5+
| ^^^^^^^^^--------------------
6+
| |
7+
| help: consider omitting `::{self}`: `fs as alias;`
68
|
79
= note: `-D clippy::unnecessary-self-imports` implied by `-D warnings`
810
= note: this will slightly change semantics; any non-module items at the same path will also be imported
911

10-
error: import ending with `self`
11-
--> $DIR/unnecessary_self_imports.rs:7:1
12+
error: import ending with `::{self}`
13+
--> $DIR/unnecessary_self_imports.rs:8:1
1214
|
13-
LL | use std::io::{self};
14-
| ^^^^^^^^^^^^^^^^^^^^ help: consider omitting `self`: `use std::io;`
15+
LL | use std::rc::{self};
16+
| ^^^^^^^^^-----------
17+
| |
18+
| help: consider omitting `::{self}`: `rc;`
1519
|
1620
= note: this will slightly change semantics; any non-module items at the same path will also be imported
1721

0 commit comments

Comments
 (0)