forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#5058 - xiongmao86:issue4903, r=flip1995
Closes Issue4903 fixes rust-lang#4903. Check list: - [x] Followed [lint naming conventions][lint_naming] - [x] Added passing UI tests (including committed `.stderr` file) - [x] `cargo test` passes locally - [x] Executed `./util/dev update_lints` - [x] Added lint documentation - [x] Run `./util/dev fmt` [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints --- changelog: implement lint that warn about single component path imports(`use ident;`).
- Loading branch information
Showing
10 changed files
with
115 additions
and
7 deletions.
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
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,61 @@ | ||
use crate::utils::span_lint_and_sugg; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::edition::Edition; | ||
use syntax::ast::{Item, ItemKind, UseTreeKind}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checking for imports with single component use path. | ||
/// | ||
/// **Why is this bad?** Import with single component use path such as `use cratename;` | ||
/// is not necessary, and thus should be removed. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust, ignore | ||
/// use regex; | ||
/// | ||
/// fn main() { | ||
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); | ||
/// } | ||
/// ``` | ||
/// Better as | ||
/// ```rust, ignore | ||
/// fn main() { | ||
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); | ||
/// } | ||
/// ``` | ||
pub SINGLE_COMPONENT_PATH_IMPORTS, | ||
style, | ||
"imports with single component path are redundant" | ||
} | ||
|
||
declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]); | ||
|
||
impl EarlyLintPass for SingleComponentPathImports { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { | ||
if_chain! { | ||
if cx.sess.opts.edition == Edition::Edition2018; | ||
if !item.vis.node.is_pub(); | ||
if let ItemKind::Use(use_tree) = &item.kind; | ||
if let segments = &use_tree.prefix.segments; | ||
if segments.len() == 1; | ||
if let UseTreeKind::Simple(None, _, _) = use_tree.kind; | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
SINGLE_COMPONENT_PATH_IMPORTS, | ||
item.span, | ||
"this import is redundant", | ||
"remove it entirely", | ||
String::new(), | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} | ||
} | ||
} |
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,12 @@ | ||
// run-rustfix | ||
// compile-flags: --edition 2018 | ||
#![warn(clippy::single_component_path_imports)] | ||
#![allow(unused_imports)] | ||
|
||
|
||
use serde as edres; | ||
pub use serde; | ||
|
||
fn main() { | ||
regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); | ||
} |
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,12 @@ | ||
// run-rustfix | ||
// compile-flags: --edition 2018 | ||
#![warn(clippy::single_component_path_imports)] | ||
#![allow(unused_imports)] | ||
|
||
use regex; | ||
use serde as edres; | ||
pub use serde; | ||
|
||
fn main() { | ||
regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap(); | ||
} |
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 @@ | ||
error: this import is redundant | ||
--> $DIR/single_component_path_imports.rs:6:1 | ||
| | ||
LL | use regex; | ||
| ^^^^^^^^^^ help: remove it entirely | ||
| | ||
= note: `-D clippy::single-component-path-imports` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|