Skip to content

Commit cf4223e

Browse files
committed
add unnecessary_self_imports lint
1 parent e9728b8 commit cf4223e

File tree

7 files changed

+110
-1
lines changed

7 files changed

+110
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,7 @@ Released 2018-09-13
25192519
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
25202520
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
25212521
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
2522+
[`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
25222523
[`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by
25232524
[`unnecessary_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
25242525
[`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
@@ -356,6 +356,7 @@ mod unicode;
356356
mod unit_return_expecting_ord;
357357
mod unit_types;
358358
mod unnamed_address;
359+
mod unnecessary_self_imports;
359360
mod unnecessary_sort_by;
360361
mod unnecessary_wraps;
361362
mod unnested_or_patterns;
@@ -963,6 +964,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
963964
unit_types::UNIT_CMP,
964965
unnamed_address::FN_ADDRESS_COMPARISONS,
965966
unnamed_address::VTABLE_ADDRESS_COMPARISONS,
967+
unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS,
966968
unnecessary_sort_by::UNNECESSARY_SORT_BY,
967969
unnecessary_wraps::UNNECESSARY_WRAPS,
968970
unnested_or_patterns::UNNESTED_OR_PATTERNS,
@@ -1048,6 +1050,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10481050
store.register_late_pass(|| box default_numeric_fallback::DefaultNumericFallback);
10491051
store.register_late_pass(|| box inconsistent_struct_constructor::InconsistentStructConstructor);
10501052
store.register_late_pass(|| box non_octal_unix_permissions::NonOctalUnixPermissions);
1053+
store.register_early_pass(|| box unnecessary_self_imports::UnnecessarySelfImports);
10511054

10521055
let msrv = conf.msrv.as_ref().and_then(|s| {
10531056
parse_msrv(s, None, None).or_else(|| {
@@ -1320,6 +1323,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13201323
LintId::of(strings::STRING_TO_STRING),
13211324
LintId::of(strings::STR_TO_STRING),
13221325
LintId::of(types::RC_BUFFER),
1326+
LintId::of(unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS),
13231327
LintId::of(unwrap_in_result::UNWRAP_IN_RESULT),
13241328
LintId::of(verbose_file_reads::VERBOSE_FILE_READS),
13251329
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet;
3+
use if_chain::if_chain;
4+
use rustc_ast::{Item, ItemKind, Path, PathSegment, UseTree, UseTreeKind};
5+
use rustc_errors::Applicability;
6+
use rustc_lint::{EarlyContext, EarlyLintPass};
7+
use rustc_session::{declare_lint_pass, declare_tool_lint};
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:** Sometimes removing `::{self}` will break code (https://github.com/rust-lang/rustfmt/issues/3568).
15+
/// This lint should not be permanently enabled.
16+
///
17+
/// **Example:**
18+
///
19+
/// ```rust
20+
/// use std::io::{self};
21+
/// ```
22+
/// Use instead:
23+
/// ```rust
24+
/// use std::io;
25+
/// ```
26+
pub UNNECESSARY_SELF_IMPORTS,
27+
restriction,
28+
"imports ending in `self`, which can be omitted"
29+
}
30+
31+
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
32+
33+
impl EarlyLintPass for UnnecessarySelfImports {
34+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
35+
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 == sym!(self);
43+
44+
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+
};
58+
span_lint_and_sugg(
59+
cx,
60+
UNNECESSARY_SELF_IMPORTS,
61+
item.span,
62+
"import ending with `self`",
63+
"consider omitting `::{self}`",
64+
snippet,
65+
Applicability::MaybeIncorrect,
66+
);
67+
}
68+
}
69+
}
70+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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;
8+
9+
fn main() {}

tests/ui/unnecessary_self_imports.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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};
8+
9+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: import ending with `self`
2+
--> $DIR/unnecessary_self_imports.rs:6:1
3+
|
4+
LL | use std::fs::{self as alias};
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `use std::fs as alias;`
6+
|
7+
= note: `-D clippy::unnecessary-self-imports` implied by `-D warnings`
8+
9+
error: import ending with `self`
10+
--> $DIR/unnecessary_self_imports.rs:7:1
11+
|
12+
LL | use std::io::{self};
13+
| ^^^^^^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `use std::io;`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)