]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/unnecessary_self_imports.rs
Auto merge of #87421 - estebank:perf-run, r=oli-obk
[rust.git] / src / tools / clippy / clippy_lints / src / unnecessary_self_imports.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
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::symbol::kw;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for imports ending in `::{self}`.
12     ///
13     /// ### Why is this bad?
14     /// In most cases, this can be written much more cleanly by omitting `::{self}`.
15     ///
16     /// ### Known problems
17     /// Removing `::{self}` will cause any non-module items at the same path to also be imported.
18     /// This might cause a naming conflict (https://github.com/rust-lang/rustfmt/issues/3568). This lint makes no attempt
19     /// to detect this scenario and that is why it is a restriction lint.
20     ///
21     /// ### Example
22     /// ```rust
23     /// use std::io::{self};
24     /// ```
25     /// Use instead:
26     /// ```rust
27     /// use std::io;
28     /// ```
29     pub UNNECESSARY_SELF_IMPORTS,
30     restriction,
31     "imports ending in `::{self}`, which can be omitted"
32 }
33
34 declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]);
35
36 impl EarlyLintPass for UnnecessarySelfImports {
37     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
38         if_chain! {
39             if let ItemKind::Use(use_tree) = &item.kind;
40             if let UseTreeKind::Nested(nodes) = &use_tree.kind;
41             if let [(self_tree, _)] = &**nodes;
42             if let [self_seg] = &*self_tree.prefix.segments;
43             if self_seg.ident.name == kw::SelfLower;
44             if let Some(last_segment) = use_tree.prefix.segments.last();
45
46             then {
47                 span_lint_and_then(
48                     cx,
49                     UNNECESSARY_SELF_IMPORTS,
50                     item.span,
51                     "import ending with `::{self}`",
52                     |diag| {
53                         diag.span_suggestion(
54                             last_segment.span().with_hi(item.span.hi()),
55                             "consider omitting `::{self}`",
56                             format!(
57                                 "{}{};",
58                                 last_segment.ident,
59                                 if let UseTreeKind::Simple(Some(alias), ..) = self_tree.kind { format!(" as {}", alias) } else { String::new() },
60                             ),
61                             Applicability::MaybeIncorrect,
62                         );
63                         diag.note("this will slightly change semantics; any non-module items at the same path will also be imported");
64                     },
65                 );
66             }
67         }
68     }
69 }