1 use crate::utils::{in_macro, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_ast::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;
10 /// **What it does:** Checking for imports with single component use path.
12 /// **Why is this bad?** Import with single component use path such as `use cratename;`
13 /// is not necessary, and thus should be removed.
15 /// **Known problems:** None.
23 /// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
29 /// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
32 pub SINGLE_COMPONENT_PATH_IMPORTS,
34 "imports with single component path are redundant"
37 declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
39 impl EarlyLintPass for SingleComponentPathImports {
40 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
42 if !in_macro(item.span);
43 if cx.sess.opts.edition == Edition::Edition2018;
44 if !item.vis.node.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;
52 SINGLE_COMPONENT_PATH_IMPORTS,
54 "this import is redundant",
57 Applicability::MachineApplicable