]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/single_component_path_imports.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / src / tools / clippy / clippy_lints / src / single_component_path_imports.rs
CommitLineData
f20569fa
XL
1use crate::utils::{in_macro, span_lint_and_sugg};
2use if_chain::if_chain;
3use rustc_ast::{Item, ItemKind, UseTreeKind};
4use rustc_errors::Applicability;
5use rustc_lint::{EarlyContext, EarlyLintPass};
6use rustc_session::{declare_lint_pass, declare_tool_lint};
7use rustc_span::edition::Edition;
8
9declare_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
37declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
38
39impl 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}