Skip to content

Commit 79b9eb5

Browse files
committed
Auto merge of #7072 - ebobrow:imports-ending-with-self, r=camsteffen
add unnecessary_self_imports lint fixes #6552 changelog: add `unnecessary_self_imports` lint
2 parents bbc22e2 + 224881b commit 79b9eb5

7 files changed

+116
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,7 @@ Released 2018-09-13
25222522
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
25232523
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
25242524
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
2525+
[`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
25252526
[`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by
25262527
[`unnecessary_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
25272528
[`unnecessary_wraps`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_wraps

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ mod unicode;
357357
mod unit_return_expecting_ord;
358358
mod unit_types;
359359
mod unnamed_address;
360+
mod unnecessary_self_imports;
360361
mod unnecessary_sort_by;
361362
mod unnecessary_wraps;
362363
mod unnested_or_patterns;
@@ -968,6 +969,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
968969
unit_types::UNIT_CMP,
969970
unnamed_address::FN_ADDRESS_COMPARISONS,
970971
unnamed_address::VTABLE_ADDRESS_COMPARISONS,
972+
unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS,
971973
unnecessary_sort_by::UNNECESSARY_SORT_BY,
972974
unnecessary_wraps::UNNECESSARY_WRAPS,
973975
unnested_or_patterns::UNNESTED_OR_PATTERNS,
@@ -1053,6 +1055,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10531055
store.register_late_pass(|| box default_numeric_fallback::DefaultNumericFallback);
10541056
store.register_late_pass(|| box inconsistent_struct_constructor::InconsistentStructConstructor);
10551057
store.register_late_pass(|| box non_octal_unix_permissions::NonOctalUnixPermissions);
1058+
store.register_early_pass(|| box unnecessary_self_imports::UnnecessarySelfImports);
10561059

10571060
let msrv = conf.msrv.as_ref().and_then(|s| {
10581061
parse_msrv(s, None, None).or_else(|| {
@@ -1326,6 +1329,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13261329
LintId::of(strings::STRING_TO_STRING),
13271330
LintId::of(strings::STR_TO_STRING),
13281331
LintId::of(types::RC_BUFFER),
1332+
LintId::of(unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS),
13291333
LintId::of(unwrap_in_result::UNWRAP_IN_RESULT),
13301334
LintId::of(verbose_file_reads::VERBOSE_FILE_READS),
13311335
LintId::of(write::PRINT_STDERR),

clippy_lints/src/modulo_arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::sext;
44
use if_chain::if_chain;
55
use rustc_hir::{BinOpKind, Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_middle::ty::{self};
7+
use rustc_middle::ty;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use std::fmt::Display;
1010

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use if_chain::if_chain;
3+
use rustc_ast::{Item, ItemKind, UseTreeKind};
4+
use rustc_errors::Applicability;
5+
use rustc_lint::{EarlyContext, EarlyLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::symbol::kw;
8+
9+
declare_clippy_lint! {
10+
/// **What it does:** Checks for imports ending in `::{self}`.
11+
///
12+
/// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `::{self}`.
13+
///
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.
17+
///
18+
/// **Example:**
19+
///
20+
/// ```rust
21+
/// use std::io::{self};
22+
/// ```
23+
/// Use instead:
24+
/// ```rust
25+
/// use std::io;
26+
/// ```
27+
pub UNNECESSARY_SELF_IMPORTS,
28+
restriction,
29+
"imports ending in `::{self}`, which can be omitted"
30+
}
31+
32+
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
33+
34+
impl EarlyLintPass for UnnecessarySelfImports {
35+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
36+
if_chain! {
37+
if let ItemKind::Use(use_tree) = &item.kind;
38+
if let UseTreeKind::Nested(nodes) = &use_tree.kind;
39+
if let [(self_tree, _)] = &**nodes;
40+
if let [self_seg] = &*self_tree.prefix.segments;
41+
if self_seg.ident.name == kw::SelfLower;
42+
if let Some(last_segment) = use_tree.prefix.segments.last();
43+
44+
then {
45+
span_lint_and_then(
46+
cx,
47+
UNNECESSARY_SELF_IMPORTS,
48+
item.span,
49+
"import ending with `::{self}`",
50+
|diag| {
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), ..) = self_tree.kind { format!(" as {}", alias) } else { String::new() },
58+
),
59+
Applicability::MaybeIncorrect,
60+
);
61+
diag.note("this will slightly change semantics; any non-module items at the same path will also be imported");
62+
},
63+
);
64+
}
65+
}
66+
}
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
#![warn(clippy::unnecessary_self_imports)]
3+
#![allow(unused_imports, dead_code)]
4+
5+
use std::collections::hash_map::{self, *};
6+
use std::fs as alias;
7+
use std::io::{self, Read};
8+
use std::rc;
9+
10+
fn main() {}

tests/ui/unnecessary_self_imports.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
#![warn(clippy::unnecessary_self_imports)]
3+
#![allow(unused_imports, dead_code)]
4+
5+
use std::collections::hash_map::{self, *};
6+
use std::fs::{self as alias};
7+
use std::io::{self, Read};
8+
use std::rc::{self};
9+
10+
fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: import ending with `::{self}`
2+
--> $DIR/unnecessary_self_imports.rs:6:1
3+
|
4+
LL | use std::fs::{self as alias};
5+
| ^^^^^^^^^--------------------
6+
| |
7+
| help: consider omitting `::{self}`: `fs as alias;`
8+
|
9+
= note: `-D clippy::unnecessary-self-imports` implied by `-D warnings`
10+
= note: this will slightly change semantics; any non-module items at the same path will also be imported
11+
12+
error: import ending with `::{self}`
13+
--> $DIR/unnecessary_self_imports.rs:8:1
14+
|
15+
LL | use std::rc::{self};
16+
| ^^^^^^^^^-----------
17+
| |
18+
| help: consider omitting `::{self}`: `rc;`
19+
|
20+
= note: this will slightly change semantics; any non-module items at the same path will also be imported
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)