-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use if_chain::if_chain; | ||
use rustc_ast::{Item, ItemKind, Path, PathSegment, UseTree, UseTreeKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for imports ending in `::{self};`. | ||
/// | ||
/// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `self`. | ||
/// | ||
/// **Known problems:** Sometimes removing `::{self}` will break code (https://github.com/rust-lang/rustfmt/issues/3568). | ||
/// This lint should not be permanently enabled. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// use std::io::{self}; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// use std::io; | ||
/// ``` | ||
pub UNNECESSARY_SELF_IMPORTS, | ||
restriction, | ||
"imports ending in `self`, which can be omitted" | ||
} | ||
|
||
declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]); | ||
|
||
impl EarlyLintPass for UnnecessarySelfImports { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { | ||
if_chain! { | ||
if let ItemKind::Use( | ||
UseTree { kind: UseTreeKind::Nested(nodes), prefix: Path { span, .. }, .. } | ||
) = &item.kind; | ||
if nodes.len() == 1; | ||
if let (UseTree { prefix, kind, .. }, _) = &nodes[0]; | ||
if let PathSegment { ident, .. } = prefix.segments[0]; | ||
if ident.name == sym!(self); | ||
|
||
then { | ||
let adjusted_span = item.span.with_hi(span.hi()); | ||
let snippet = if let UseTreeKind::Simple(Some(alias), ..) = kind { | ||
format!( | ||
"{} as {};", | ||
snippet(cx, adjusted_span, ".."), | ||
snippet(cx, alias.span, "..") | ||
) | ||
} else { | ||
format!( | ||
"{};", | ||
snippet(cx, adjusted_span, "..") | ||
) | ||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_SELF_IMPORTS, | ||
item.span, | ||
"import ending with `self`", | ||
"consider omitting `::{self}`", | ||
snippet, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
#![warn(clippy::unnecessary_self_imports)] | ||
#![allow(unused_imports, dead_code)] | ||
|
||
use std::collections::hash_map::{self, *}; | ||
use std::fs as alias; | ||
use std::io; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
#![warn(clippy::unnecessary_self_imports)] | ||
#![allow(unused_imports, dead_code)] | ||
|
||
use std::collections::hash_map::{self, *}; | ||
use std::fs::{self as alias}; | ||
use std::io::{self}; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: import ending with `self` | ||
--> $DIR/unnecessary_self_imports.rs:6:1 | ||
| | ||
LL | use std::fs::{self as alias}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `use std::fs as alias;` | ||
| | ||
= note: `-D clippy::unnecessary-self-imports` implied by `-D warnings` | ||
|
||
error: import ending with `self` | ||
--> $DIR/unnecessary_self_imports.rs:7:1 | ||
| | ||
LL | use std::io::{self}; | ||
| ^^^^^^^^^^^^^^^^^^^^ help: consider omitting `::{self}`: `use std::io;` | ||
|
||
error: aborting due to 2 previous errors | ||
|