Skip to content

Commit f69835b

Browse files
committed
Auto merge of #5058 - xiongmao86:issue4903, r=flip1995
Closes Issue4903 fixes #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;`).
2 parents c0f39cf + ac9f019 commit f69835b

10 files changed

+115
-7
lines changed

CHANGELOG.md

+1
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

+1-1
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

+5
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),

clippy_lints/src/methods/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2310,7 +2310,7 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
23102310
&format!("used `unwrap()` on `{}` value", kind,),
23112311
&format!(
23122312
"if you don't want to handle the `{}` case gracefully, consider \
2313-
using `expect()` to provide a better panic message",
2313+
using `expect()` to provide a better panic message",
23142314
none_value,
23152315
),
23162316
);
@@ -2679,7 +2679,7 @@ fn lint_filter_flat_map<'a, 'tcx>(
26792679
if match_trait_method(cx, expr, &paths::ITERATOR) {
26802680
let msg = "called `filter(p).flat_map(q)` on an `Iterator`";
26812681
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2682-
and filtering by returning `iter::empty()`";
2682+
and filtering by returning `iter::empty()`";
26832683
span_lint_and_help(cx, FILTER_MAP, expr.span, msg, hint);
26842684
}
26852685
}
@@ -2695,7 +2695,7 @@ fn lint_filter_map_flat_map<'a, 'tcx>(
26952695
if match_trait_method(cx, expr, &paths::ITERATOR) {
26962696
let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`";
26972697
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2698-
and filtering by returning `iter::empty()`";
2698+
and filtering by returning `iter::empty()`";
26992699
span_lint_and_help(cx, FILTER_MAP, expr.span, msg, hint);
27002700
}
27012701
}
@@ -3148,7 +3148,7 @@ fn lint_option_as_ref_deref<'a, 'tcx>(
31483148

31493149
let msg = format!(
31503150
"called `{0}` (or with one of deref aliases) on an Option value. \
3151-
This can be done more directly by calling `{1}` instead",
3151+
This can be done more directly by calling `{1}` instead",
31523152
current_method, hint
31533153
);
31543154
span_lint_and_sugg(
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+
}

clippy_lints/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ impl Types {
316316
OPTION_OPTION,
317317
hir_ty.span,
318318
"consider using `Option<T>` instead of `Option<Option<T>>` or a custom \
319-
enum if you need to distinguish all 3 cases",
319+
enum if you need to distinguish all 3 cases",
320320
);
321321
return; // don't recurse into the type
322322
}

src/lintlist/mod.rs

+8-1
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",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-rustfix
2+
// compile-flags: --edition 2018
3+
#![warn(clippy::single_component_path_imports)]
4+
#![allow(unused_imports)]
5+
6+
7+
use serde as edres;
8+
pub use serde;
9+
10+
fn main() {
11+
regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-rustfix
2+
// compile-flags: --edition 2018
3+
#![warn(clippy::single_component_path_imports)]
4+
#![allow(unused_imports)]
5+
6+
use regex;
7+
use serde as edres;
8+
pub use serde;
9+
10+
fn main() {
11+
regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: this import is redundant
2+
--> $DIR/single_component_path_imports.rs:6:1
3+
|
4+
LL | use regex;
5+
| ^^^^^^^^^^ help: remove it entirely
6+
|
7+
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)