]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | use crate::utils::{in_macro, span_lint_and_sugg}; |
2 | use if_chain::if_chain; | |
3 | use rustc_ast::{Item, ItemKind, UseTreeKind}; | |
4 | use rustc_errors::Applicability; | |
5 | use rustc_lint::{EarlyContext, EarlyLintPass}; | |
6 | use rustc_session::{declare_lint_pass, declare_tool_lint}; | |
7 | use rustc_span::edition::Edition; | |
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 !in_macro(item.span); | |
43 | if cx.sess.opts.edition >= Edition::Edition2018; | |
44 | if !item.vis.kind.is_pub(); | |
45 | if let ItemKind::Use(use_tree) = &item.kind; | |
46 | if let segments = &use_tree.prefix.segments; | |
47 | if segments.len() == 1; | |
48 | if let UseTreeKind::Simple(None, _, _) = use_tree.kind; | |
49 | then { | |
50 | span_lint_and_sugg( | |
51 | cx, | |
52 | SINGLE_COMPONENT_PATH_IMPORTS, | |
53 | item.span, | |
54 | "this import is redundant", | |
55 | "remove it entirely", | |
56 | String::new(), | |
57 | Applicability::MachineApplicable | |
58 | ); | |
59 | } | |
60 | } | |
61 | } | |
62 | } |