|
| 1 | +use clippy_config::msrvs::{self, Msrv}; |
| 2 | +use clippy_config::Conf; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 4 | +use clippy_utils::is_from_proc_macro; |
| 5 | +use clippy_utils::source::snippet_opt; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::def::{DefKind, Res}; |
| 8 | +use rustc_hir::{Item, ItemKind, UseKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass, LintContext as _}; |
| 10 | +use rustc_middle::lint::in_external_macro; |
| 11 | +use rustc_middle::ty::Visibility; |
| 12 | +use rustc_session::impl_lint_pass; |
| 13 | +use rustc_span::symbol::kw; |
| 14 | + |
| 15 | +declare_clippy_lint! { |
| 16 | + /// ### What it does |
| 17 | + /// Checks for `use Trait` where the Trait is only used for its methods and not referenced by a path directly. |
| 18 | + /// |
| 19 | + /// ### Why is this bad? |
| 20 | + /// Traits imported that aren't used directly can be imported anonymously with `use Trait as _`. |
| 21 | + /// It is more explicit, avoids polluting the current scope with unused names and can be useful to show which imports are required for traits. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```no_run |
| 25 | + /// use std::fmt::Write; |
| 26 | + /// |
| 27 | + /// fn main() { |
| 28 | + /// let mut s = String::new(); |
| 29 | + /// let _ = write!(s, "hello, world!"); |
| 30 | + /// println!("{s}"); |
| 31 | + /// } |
| 32 | + /// ``` |
| 33 | + /// Use instead: |
| 34 | + /// ```no_run |
| 35 | + /// use std::fmt::Write as _; |
| 36 | + /// |
| 37 | + /// fn main() { |
| 38 | + /// let mut s = String::new(); |
| 39 | + /// let _ = write!(s, "hello, world!"); |
| 40 | + /// println!("{s}"); |
| 41 | + /// } |
| 42 | + /// ``` |
| 43 | + #[clippy::version = "1.83.0"] |
| 44 | + pub UNUSED_TRAIT_NAMES, |
| 45 | + restriction, |
| 46 | + "use items that import a trait but only use it anonymously" |
| 47 | +} |
| 48 | + |
| 49 | +pub struct UnusedTraitNames { |
| 50 | + msrv: Msrv, |
| 51 | +} |
| 52 | + |
| 53 | +impl UnusedTraitNames { |
| 54 | + pub fn new(conf: &'static Conf) -> Self { |
| 55 | + Self { |
| 56 | + msrv: conf.msrv.clone(), |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl_lint_pass!(UnusedTraitNames => [UNUSED_TRAIT_NAMES]); |
| 62 | + |
| 63 | +impl<'tcx> LateLintPass<'tcx> for UnusedTraitNames { |
| 64 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 65 | + if self.msrv.meets(msrvs::UNDERSCORE_IMPORTS) |
| 66 | + && !in_external_macro(cx.sess(), item.span) |
| 67 | + && let ItemKind::Use(path, UseKind::Single) = item.kind |
| 68 | + // Ignore imports that already use Underscore |
| 69 | + && item.ident.name != kw::Underscore |
| 70 | + // Only check traits |
| 71 | + && let Some(Res::Def(DefKind::Trait, _)) = path.res.first() |
| 72 | + && cx.tcx.maybe_unused_trait_imports(()).contains(&item.owner_id.def_id) |
| 73 | + // Only check this import if it is visible to its module only (no pub, pub(crate), ...) |
| 74 | + && let module = cx.tcx.parent_module_from_def_id(item.owner_id.def_id) |
| 75 | + && cx.tcx.visibility(item.owner_id.def_id) == Visibility::Restricted(module.to_def_id()) |
| 76 | + && let Some(last_segment) = path.segments.last() |
| 77 | + && let Some(snip) = snippet_opt(cx, last_segment.ident.span) |
| 78 | + && !is_from_proc_macro(cx, &last_segment.ident) |
| 79 | + { |
| 80 | + let complete_span = last_segment.ident.span.to(item.ident.span); |
| 81 | + span_lint_and_sugg( |
| 82 | + cx, |
| 83 | + UNUSED_TRAIT_NAMES, |
| 84 | + complete_span, |
| 85 | + "importing trait that is only used anonymously", |
| 86 | + "use", |
| 87 | + format!("{snip} as _"), |
| 88 | + Applicability::MachineApplicable, |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + extract_msrv_attr!(LateContext); |
| 94 | +} |
0 commit comments