-
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
116 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,67 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use if_chain::if_chain; | ||
use rustc_ast::{Item, ItemKind, UseTreeKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::symbol::kw; | ||
|
||
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:** Removing `::{self}` will cause any non-module items at the same path to also be imported. | ||
/// This might cause a naming conflict (https://github.com/rust-lang/rustfmt/issues/3568). This lint makes no attempt | ||
/// to detect this scenario and that is why it is a restriction lint. | ||
/// | ||
/// **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(use_tree) = &item.kind; | ||
if let UseTreeKind::Nested(nodes) = &use_tree.kind; | ||
if let [(self_tree, _)] = &**nodes; | ||
if let [self_seg] = &*self_tree.prefix.segments; | ||
if self_seg.ident.name == kw::SelfLower; | ||
if let Some(last_segment) = use_tree.prefix.segments.last(); | ||
|
||
then { | ||
span_lint_and_then( | ||
cx, | ||
UNNECESSARY_SELF_IMPORTS, | ||
item.span, | ||
"import ending with `::{self}`", | ||
|diag| { | ||
diag.span_suggestion( | ||
last_segment.span().with_hi(item.span.hi()), | ||
"consider omitting `::{self}`", | ||
format!( | ||
"{}{};", | ||
last_segment.ident, | ||
if let UseTreeKind::Simple(Some(alias), ..) = self_tree.kind { format!(" as {}", alias) } else { String::new() }, | ||
), | ||
Applicability::MaybeIncorrect, | ||
); | ||
diag.note("this will slightly change semantics; any non-module items at the same path will also be imported"); | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
} |
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,10 @@ | ||
// 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::{self, Read}; | ||
use std::rc; | ||
|
||
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,10 @@ | ||
// 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, Read}; | ||
use std::rc::{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,23 @@ | ||
error: import ending with `::{self}` | ||
--> $DIR/unnecessary_self_imports.rs:6:1 | ||
| | ||
LL | use std::fs::{self as alias}; | ||
| ^^^^^^^^^-------------------- | ||
| | | ||
| help: consider omitting `::{self}`: `fs as alias;` | ||
| | ||
= note: `-D clippy::unnecessary-self-imports` implied by `-D warnings` | ||
= note: this will slightly change semantics; any non-module items at the same path will also be imported | ||
|
||
error: import ending with `::{self}` | ||
--> $DIR/unnecessary_self_imports.rs:8:1 | ||
| | ||
LL | use std::rc::{self}; | ||
| ^^^^^^^^^----------- | ||
| | | ||
| help: consider omitting `::{self}`: `rc;` | ||
| | ||
= note: this will slightly change semantics; any non-module items at the same path will also be imported | ||
|
||
error: aborting due to 2 previous errors | ||
|