|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::trait_ref_of_method; |
| 3 | +use rustc_data_structures::fx::FxHashMap; |
| 4 | +use rustc_hir::intravisit::{walk_impl_item, walk_item, walk_param_bound, walk_ty, Visitor}; |
| 5 | +use rustc_hir::{GenericParamKind, Generics, ImplItem, ImplItemKind, Item, ItemKind, Ty, WherePredicate}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_middle::hir::nested_filter; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | +use rustc_span::{def_id::DefId, Span}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// ### What it does |
| 13 | + /// Checks for type parameters in generics that are never used anywhere else. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// Functions cannot infer the value of unused type parameters; therefore, calling them |
| 17 | + /// requires using a turbofish, which serves no purpose but to satisfy the compiler. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```rust |
| 21 | + /// // unused type parameters |
| 22 | + /// fn unused_ty<T>(x: u8) { |
| 23 | + /// // .. |
| 24 | + /// } |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```rust |
| 28 | + /// fn no_unused_ty(x: u8) { |
| 29 | + /// // .. |
| 30 | + /// } |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.67.0"] |
| 33 | + pub EXTRA_UNUSED_TYPE_PARAMETERS, |
| 34 | + complexity, |
| 35 | + "unused type parameters in function definitions" |
| 36 | +} |
| 37 | +declare_lint_pass!(ExtraUnusedTypeParameters => [EXTRA_UNUSED_TYPE_PARAMETERS]); |
| 38 | + |
| 39 | +struct TypeWalker<'cx, 'tcx> { |
| 40 | + cx: &'cx LateContext<'tcx>, |
| 41 | + map: FxHashMap<DefId, Span>, |
| 42 | +} |
| 43 | + |
| 44 | +impl<'cx, 'tcx> TypeWalker<'cx, 'tcx> { |
| 45 | + fn new(cx: &'cx LateContext<'tcx>, generics: &Generics<'tcx>) -> Self { |
| 46 | + Self { |
| 47 | + cx, |
| 48 | + map: generics |
| 49 | + .params |
| 50 | + .iter() |
| 51 | + .filter_map(|param| match param.kind { |
| 52 | + GenericParamKind::Type { .. } => Some((param.def_id.into(), param.span)), |
| 53 | + _ => None, |
| 54 | + }) |
| 55 | + .collect(), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fn emit_lint(&self) { |
| 60 | + for span in self.map.values() { |
| 61 | + span_lint( |
| 62 | + self.cx, |
| 63 | + EXTRA_UNUSED_TYPE_PARAMETERS, |
| 64 | + *span, |
| 65 | + "type parameter goes unused in function definition", |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> { |
| 72 | + type NestedFilter = nested_filter::OnlyBodies; |
| 73 | + |
| 74 | + fn visit_ty(&mut self, t: &'tcx Ty<'tcx>) { |
| 75 | + if let Some((def_id, _)) = t.peel_refs().as_generic_param() { |
| 76 | + self.map.remove(&def_id); |
| 77 | + } else { |
| 78 | + walk_ty(self, t); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fn visit_where_predicate(&mut self, predicate: &'tcx WherePredicate<'tcx>) { |
| 83 | + if let WherePredicate::BoundPredicate(where_bound_predicate) = predicate { |
| 84 | + // Only check the right-hand side of where-bounds |
| 85 | + for bound in where_bound_predicate.bounds { |
| 86 | + walk_param_bound(self, bound); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fn nested_visit_map(&mut self) -> Self::Map { |
| 92 | + self.cx.tcx.hir() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters { |
| 97 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 98 | + if let ItemKind::Fn(_, generics, _) = item.kind { |
| 99 | + let mut walker = TypeWalker::new(cx, generics); |
| 100 | + walk_item(&mut walker, item); |
| 101 | + walker.emit_lint(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { |
| 106 | + if let ImplItemKind::Fn(..) = item.kind && trait_ref_of_method(cx, item.owner_id.def_id).is_none() { |
| 107 | + let mut walker = TypeWalker::new(cx, item.generics); |
| 108 | + walk_impl_item(&mut walker, item); |
| 109 | + walker.emit_lint(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments