1 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::in_macro;
3 use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, 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, symbol::kw, Span, Symbol};
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_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
41 if cx.sess.opts.edition < Edition::Edition2018 {
44 check_mod(cx, &krate.items);
48 fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
49 // keep track of imports reused with `self` keyword,
50 // such as `self::crypto_hash` in the example below
52 // use self::crypto_hash::{Algorithm, Hasher};
54 let mut imports_reused_with_self = Vec::new();
56 // keep track of single use statements
57 // such as `crypto_hash` in the example below
61 let mut single_use_usages = Vec::new();
64 track_uses(cx, &item, &mut imports_reused_with_self, &mut single_use_usages);
67 for single_use in &single_use_usages {
68 if !imports_reused_with_self.contains(&single_use.0) {
69 let can_suggest = single_use.2;
73 SINGLE_COMPONENT_PATH_IMPORTS,
75 "this import is redundant",
78 Applicability::MachineApplicable,
83 SINGLE_COMPONENT_PATH_IMPORTS,
85 "this import is redundant",
95 cx: &EarlyContext<'_>,
97 imports_reused_with_self: &mut Vec<Symbol>,
98 single_use_usages: &mut Vec<(Symbol, Span, bool)>,
100 if in_macro(item.span) || item.vis.kind.is_pub() {
105 ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
106 check_mod(cx, &items);
108 ItemKind::Use(use_tree) => {
109 let segments = &use_tree.prefix.segments;
111 // keep track of `use some_module;` usages
112 if segments.len() == 1 {
113 if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
114 let ident = &segments[0].ident;
115 single_use_usages.push((ident.name, item.span, true));
120 if segments.is_empty() {
121 // keep track of `use {some_module, some_other_module};` usages
122 if let UseTreeKind::Nested(trees) = &use_tree.kind {
124 let segments = &tree.0.prefix.segments;
125 if segments.len() == 1 {
126 if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
127 let ident = &segments[0].ident;
128 single_use_usages.push((ident.name, tree.0.span, false));
134 // keep track of `use self::some_module` usages
135 if segments[0].ident.name == kw::SelfLower {
136 // simple case such as `use self::module::SomeStruct`
137 if segments.len() > 1 {
138 imports_reused_with_self.push(segments[1].ident.name);
142 // nested case such as `use self::{module1::Struct1, module2::Struct2}`
143 if let UseTreeKind::Nested(trees) = &use_tree.kind {
145 let segments = &tree.0.prefix.segments;
146 if !segments.is_empty() {
147 imports_reused_with_self.push(segments[0].ident.name);