Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 512efbe

Browse files
committed
Declare lint and implement lint logic.
1 parent c0f39cf commit 512efbe

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ Released 2018-09-13
12841284
[`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
12851285
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
12861286
[`single_char_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
1287+
[`single_component_path_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports
12871288
[`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match
12881289
[`single_match_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else
12891290
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 349 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 350 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ pub mod replace_consts;
284284
pub mod returns;
285285
pub mod serde_api;
286286
pub mod shadow;
287+
pub mod single_component_path_imports;
287288
pub mod slow_vector_initialization;
288289
pub mod strings;
289290
pub mod suspicious_trait_impl;
@@ -741,6 +742,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
741742
&shadow::SHADOW_REUSE,
742743
&shadow::SHADOW_SAME,
743744
&shadow::SHADOW_UNRELATED,
745+
&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS,
744746
&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
745747
&strings::STRING_ADD,
746748
&strings::STRING_ADD_ASSIGN,
@@ -993,6 +995,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
993995
store.register_early_pass(|| box utils::internal_lints::ProduceIce);
994996
store.register_late_pass(|| box let_underscore::LetUnderscore);
995997
store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
998+
store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports);
996999

9971000
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
9981001
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
@@ -1296,6 +1299,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12961299
LintId::of(&returns::NEEDLESS_RETURN),
12971300
LintId::of(&returns::UNUSED_UNIT),
12981301
LintId::of(&serde_api::SERDE_API_MISUSE),
1302+
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
12991303
LintId::of(&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
13001304
LintId::of(&strings::STRING_LIT_AS_BYTES),
13011305
LintId::of(&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
@@ -1431,6 +1435,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14311435
LintId::of(&returns::LET_AND_RETURN),
14321436
LintId::of(&returns::NEEDLESS_RETURN),
14331437
LintId::of(&returns::UNUSED_UNIT),
1438+
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
14341439
LintId::of(&strings::STRING_LIT_AS_BYTES),
14351440
LintId::of(&tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
14361441
LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME),
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::utils::span_lint_and_sugg;
2+
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
4+
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
use rustc_span::edition::Edition;
7+
use syntax::ast::{Item, ItemKind, UseTreeKind};
8+
9+
declare_clippy_lint! {
10+
/// **What it does:** Checking for imports with single component use path.
11+
///
12+
/// **Why is this bad?** Import with single component use path such as `use cratename;`
13+
/// is not necessary, and thus should be removed.
14+
///
15+
/// **Known problems:** None.
16+
///
17+
/// **Example:**
18+
///
19+
/// ```rust, ignore
20+
/// use regex;
21+
///
22+
/// fn main() {
23+
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
24+
/// }
25+
/// ```
26+
/// Better as
27+
/// ```rust, ignore
28+
/// fn main() {
29+
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
30+
/// }
31+
/// ```
32+
pub SINGLE_COMPONENT_PATH_IMPORTS,
33+
style,
34+
"imports with single component path are redundant"
35+
}
36+
37+
declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
38+
39+
impl EarlyLintPass for SingleComponentPathImports {
40+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
41+
if_chain! {
42+
if cx.sess.opts.edition == Edition::Edition2018;
43+
if !item.vis.node.is_pub();
44+
if let ItemKind::Use(use_tree) = &item.kind;
45+
if let segments = &use_tree.prefix.segments;
46+
if segments.len() == 1;
47+
if let UseTreeKind::Simple(None, _, _) = use_tree.kind;
48+
then {
49+
span_lint_and_sugg(
50+
cx,
51+
SINGLE_COMPONENT_PATH_IMPORTS,
52+
item.span,
53+
"this import is redundant",
54+
"remove it entirely",
55+
String::new(),
56+
Applicability::MachineApplicable
57+
);
58+
}
59+
}
60+
}
61+
}

src/lintlist/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 349] = [
9+
pub const ALL_LINTS: [Lint; 350] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1855,6 +1855,13 @@ pub const ALL_LINTS: [Lint; 349] = [
18551855
deprecation: None,
18561856
module: "methods",
18571857
},
1858+
Lint {
1859+
name: "single_component_path_imports",
1860+
group: "style",
1861+
desc: "imports with single component path are redundant",
1862+
deprecation: None,
1863+
module: "single_component_path_imports",
1864+
},
18581865
Lint {
18591866
name: "single_match",
18601867
group: "style",

0 commit comments

Comments
 (0)