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, MacroDef, ModKind, UseTreeKind, VisibilityKind};
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();
63 // keep track of macros defined in the module as we don't want it to trigger on this (#7106)
65 // macro_rules! foo { () => {} };
66 // pub(crate) use foo;
68 let mut macros = Vec::new();
74 &mut imports_reused_with_self,
75 &mut single_use_usages,
80 for single_use in &single_use_usages {
81 if !imports_reused_with_self.contains(&single_use.0) {
82 let can_suggest = single_use.2;
86 SINGLE_COMPONENT_PATH_IMPORTS,
88 "this import is redundant",
91 Applicability::MachineApplicable,
96 SINGLE_COMPONENT_PATH_IMPORTS,
98 "this import is redundant",
100 "remove this import",
108 cx: &EarlyContext<'_>,
110 imports_reused_with_self: &mut Vec<Symbol>,
111 single_use_usages: &mut Vec<(Symbol, Span, bool)>,
112 macros: &mut Vec<Symbol>,
114 if in_macro(item.span) || item.vis.kind.is_pub() {
119 ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
120 check_mod(cx, items);
122 ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => {
123 macros.push(item.ident.name);
125 ItemKind::Use(use_tree) => {
126 let segments = &use_tree.prefix.segments;
129 |name: &Symbol| !macros.contains(name) || matches!(item.vis.kind, VisibilityKind::Inherited);
131 // keep track of `use some_module;` usages
132 if segments.len() == 1 {
133 if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
134 let name = segments[0].ident.name;
135 if should_report(&name) {
136 single_use_usages.push((name, item.span, true));
142 if segments.is_empty() {
143 // keep track of `use {some_module, some_other_module};` usages
144 if let UseTreeKind::Nested(trees) = &use_tree.kind {
146 let segments = &tree.0.prefix.segments;
147 if segments.len() == 1 {
148 if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
149 let name = segments[0].ident.name;
150 if should_report(&name) {
151 single_use_usages.push((name, tree.0.span, false));
158 // keep track of `use self::some_module` usages
159 if segments[0].ident.name == kw::SelfLower {
160 // simple case such as `use self::module::SomeStruct`
161 if segments.len() > 1 {
162 imports_reused_with_self.push(segments[1].ident.name);
166 // nested case such as `use self::{module1::Struct1, module2::Struct2}`
167 if let UseTreeKind::Nested(trees) = &use_tree.kind {
169 let segments = &tree.0.prefix.segments;
170 if !segments.is_empty() {
171 imports_reused_with_self.push(segments[0].ident.name);