|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::in_macro; |
| 3 | +use clippy_utils::source::snippet_with_applicability; |
| 4 | +use if_chain::if_chain; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::def::{DefKind, Res}; |
| 7 | +use rustc_hir::{Item, ItemKind, Mod, Node, Path, UseKind, VisibilityKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | +use rustc_span::def_id::DefId; |
| 11 | +use rustc_span::symbol::Ident; |
| 12 | +use rustc_span::{BytePos, Span}; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// **What it does:** Checks for imports ending in `::{self};`. |
| 16 | + /// |
| 17 | + /// **Why is this bad?** In most cases, this can be written much more cleanly by omitting `self`. |
| 18 | + /// |
| 19 | + /// **Known problems:** None. |
| 20 | + /// |
| 21 | + /// **Example:** |
| 22 | + /// |
| 23 | + /// ```rust |
| 24 | + /// use std::io::{self}; |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```rust |
| 28 | + /// use std::io; |
| 29 | + /// ``` |
| 30 | + pub UNNECESSARY_SELF_IMPORTS, |
| 31 | + style, |
| 32 | + "imports ending in `self`, which can be omitted" |
| 33 | +} |
| 34 | + |
| 35 | +declare_lint_pass!(UnnecessarySelfImports => [UNNECESSARY_SELF_IMPORTS]); |
| 36 | + |
| 37 | +impl<'tcx> LateLintPass<'tcx> for UnnecessarySelfImports { |
| 38 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { |
| 39 | + if_chain! { |
| 40 | + if !in_macro(item.span); |
| 41 | + if let ItemKind::Use(path, UseKind::ListStem) = &item.kind; |
| 42 | + if let Some(ident) = is_self_import(cx, item, path); |
| 43 | + if let Some(Res::Def(DefKind::Mod, def_id)) = path.segments[path.segments.len() - 2].res; |
| 44 | + if let Some(last) = path.segments.last(); |
| 45 | + if !mod_contains_item(cx, def_id, last.ident) || !item_is_in_scope(cx, ident, path.span); |
| 46 | + then { |
| 47 | + let mut applicability = Applicability::MachineApplicable; |
| 48 | + let snippet = if ident == path.segments[path.segments.len() - 1].ident { |
| 49 | + let adjusted_span = path.span.with_hi(path.span.hi() - BytePos(8)); |
| 50 | + format!( |
| 51 | + "{}", |
| 52 | + snippet_with_applicability(cx, adjusted_span, "..", &mut applicability) |
| 53 | + ) |
| 54 | + } else { |
| 55 | + let adjusted_span = path.span.until(ident.span); |
| 56 | + let adjusted_span = adjusted_span.with_hi(adjusted_span.hi() - BytePos(11)); |
| 57 | + format!( |
| 58 | + "{} as {}", |
| 59 | + snippet_with_applicability(cx, adjusted_span, "..", &mut applicability), |
| 60 | + snippet_with_applicability(cx, ident.span, "..", &mut applicability) |
| 61 | + ) |
| 62 | + }; |
| 63 | + span_lint_and_sugg( |
| 64 | + cx, |
| 65 | + UNNECESSARY_SELF_IMPORTS, |
| 66 | + path.span, |
| 67 | + "import ending with `self`", |
| 68 | + "consider omitting `::{self}`", |
| 69 | + snippet, |
| 70 | + applicability, |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn is_self_import(cx: &LateContext<'_>, current_import: &Item<'_>, current_import_path: &Path<'_>) -> Option<Ident> { |
| 78 | + let definitions = cx.tcx.hir().definitions(); |
| 79 | + let mut amt = 0; |
| 80 | + let mut ident: Option<Ident> = None; |
| 81 | + |
| 82 | + for def in definitions.iter_local_def_id() { |
| 83 | + if_chain! { |
| 84 | + if let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(def.to_def_id()); |
| 85 | + if let ItemKind::Use(path, kind) = item.kind; |
| 86 | + if matches!(kind, UseKind::Single | UseKind::Glob); |
| 87 | + if current_import.span.contains(item.span); |
| 88 | + then { |
| 89 | + amt += 1; |
| 90 | + if amt > 1 { return None; } |
| 91 | + |
| 92 | + if_chain! { |
| 93 | + if current_import_path.segments.len() == path.segments.len(); |
| 94 | + let current_import_last = ¤t_import_path.segments[current_import_path.segments.len() - 1]; |
| 95 | + let item_last = &path.segments[path.segments.len() - 1]; |
| 96 | + if current_import_last.ident == item_last.ident; |
| 97 | + then { |
| 98 | + ident = Some(item.ident); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + ident |
| 105 | +} |
| 106 | + |
| 107 | +fn mod_contains_item(cx: &LateContext<'_>, def_id: DefId, ident: Ident) -> bool { |
| 108 | + if let Some(Node::Item(node)) = cx.tcx.hir().get_if_local(def_id) { |
| 109 | + if let ItemKind::Mod(Mod { item_ids, .. }) = &node.kind { |
| 110 | + for item in item_ids.iter() { |
| 111 | + if_chain! { |
| 112 | + if let Some(Node::Item(node)) = cx.tcx.hir().get_if_local(item.def_id.to_def_id()); |
| 113 | + if let VisibilityKind::Public = node.vis.node; |
| 114 | + if node.ident == ident; |
| 115 | + if matches!(node.kind, ItemKind::Fn(..) | ItemKind::Const(..) | ItemKind::Static(..)); |
| 116 | + then { return true; } |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } else { |
| 122 | + for item in cx.tcx.item_children(def_id).iter() { |
| 123 | + if_chain! { |
| 124 | + if item.ident == ident; |
| 125 | + if matches!(item.res, Res::Def(DefKind::Fn | DefKind::Const | DefKind::Static, _)); |
| 126 | + then { return true; } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + false |
| 131 | +} |
| 132 | + |
| 133 | +fn item_is_in_scope(cx: &LateContext<'_>, ident: Ident, current_import: Span) -> bool { |
| 134 | + let definitions = cx.tcx.hir().definitions(); |
| 135 | + let mut outer_mod: Option<Span> = None; |
| 136 | + |
| 137 | + for def in definitions.iter_local_def_id() { |
| 138 | + if let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(def.to_def_id()) { |
| 139 | + let should_check = match outer_mod { |
| 140 | + Some(curr_mod) => { |
| 141 | + if curr_mod.contains(item.span) { |
| 142 | + false |
| 143 | + } else { |
| 144 | + outer_mod = None; |
| 145 | + true |
| 146 | + } |
| 147 | + }, |
| 148 | + None => true, |
| 149 | + }; |
| 150 | + |
| 151 | + if should_check { |
| 152 | + match item.kind { |
| 153 | + ItemKind::Fn(..) | ItemKind::Const(..) | ItemKind::Static(..) => { |
| 154 | + if item.ident == ident { |
| 155 | + return true; |
| 156 | + } |
| 157 | + }, |
| 158 | + ItemKind::Use(path, kind) => { |
| 159 | + if_chain! { |
| 160 | + if let UseKind::Single = kind; |
| 161 | + if !current_import.contains(path.span); |
| 162 | + if item.ident == ident; |
| 163 | + |
| 164 | + then { |
| 165 | + return true; |
| 166 | + } |
| 167 | + } |
| 168 | + }, |
| 169 | + _ => { |
| 170 | + outer_mod = Some(item.span); |
| 171 | + }, |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + false |
| 177 | +} |
0 commit comments