|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::trait_ref_of_method; |
| 3 | +use rustc_data_structures::fx::FxHashMap; |
| 4 | +use rustc_errors::MultiSpan; |
| 5 | +use rustc_hir::intravisit::{walk_impl_item, walk_item, walk_param_bound, walk_ty, Visitor}; |
| 6 | +use rustc_hir::{ |
| 7 | + GenericParamKind, Generics, ImplItem, ImplItemKind, Item, ItemKind, PredicateOrigin, Ty, TyKind, WherePredicate, |
| 8 | +}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_middle::hir::nested_filter; |
| 11 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 12 | +use rustc_span::{def_id::DefId, Span}; |
| 13 | + |
| 14 | +declare_clippy_lint! { |
| 15 | + /// ### What it does |
| 16 | + /// Checks for type parameters in generics that are never used anywhere else. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// Functions cannot infer the value of unused type parameters; therefore, calling them |
| 20 | + /// requires using a turbofish, which serves no purpose but to satisfy the compiler. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust |
| 24 | + /// // unused type parameters |
| 25 | + /// fn unused_ty<T>(x: u8) { |
| 26 | + /// // .. |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```rust |
| 31 | + /// fn no_unused_ty(x: u8) { |
| 32 | + /// // .. |
| 33 | + /// } |
| 34 | + /// ``` |
| 35 | + #[clippy::version = "1.69.0"] |
| 36 | + pub EXTRA_UNUSED_TYPE_PARAMETERS, |
| 37 | + complexity, |
| 38 | + "unused type parameters in function definitions" |
| 39 | +} |
| 40 | +declare_lint_pass!(ExtraUnusedTypeParameters => [EXTRA_UNUSED_TYPE_PARAMETERS]); |
| 41 | + |
| 42 | +/// A visitor struct that walks a given function and gathers generic type parameters, plus any |
| 43 | +/// trait bounds those parameters have. |
| 44 | +struct TypeWalker<'cx, 'tcx> { |
| 45 | + cx: &'cx LateContext<'tcx>, |
| 46 | + /// Collection of all the type parameters and their spans. |
| 47 | + ty_params: FxHashMap<DefId, Span>, |
| 48 | + /// Collection of any (inline) trait bounds corresponding to each type parameter. |
| 49 | + bounds: FxHashMap<DefId, Span>, |
| 50 | + /// The entire `Generics` object of the function, useful for querying purposes. |
| 51 | + generics: &'tcx Generics<'tcx>, |
| 52 | + /// The value of this will remain `true` if *every* parameter: |
| 53 | + /// 1. Is a type parameter, and |
| 54 | + /// 2. Goes unused in the function. |
| 55 | + /// Otherwise, if any type parameters end up being used, or if any lifetime or const-generic |
| 56 | + /// parameters are present, this will be set to `false`. |
| 57 | + all_params_unused: bool, |
| 58 | +} |
| 59 | + |
| 60 | +impl<'cx, 'tcx> TypeWalker<'cx, 'tcx> { |
| 61 | + fn new(cx: &'cx LateContext<'tcx>, generics: &'tcx Generics<'tcx>) -> Self { |
| 62 | + let mut all_params_unused = true; |
| 63 | + let ty_params = generics |
| 64 | + .params |
| 65 | + .iter() |
| 66 | + .filter_map(|param| { |
| 67 | + if let GenericParamKind::Type { .. } = param.kind { |
| 68 | + Some((param.def_id.into(), param.span)) |
| 69 | + } else { |
| 70 | + if !param.is_elided_lifetime() { |
| 71 | + all_params_unused = false; |
| 72 | + } |
| 73 | + None |
| 74 | + } |
| 75 | + }) |
| 76 | + .collect(); |
| 77 | + Self { |
| 78 | + cx, |
| 79 | + ty_params, |
| 80 | + bounds: FxHashMap::default(), |
| 81 | + generics, |
| 82 | + all_params_unused, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn emit_lint(&self) { |
| 87 | + let (msg, help) = match self.ty_params.len() { |
| 88 | + 0 => return, |
| 89 | + 1 => ( |
| 90 | + "type parameter goes unused in function definition", |
| 91 | + "consider removing the parameter", |
| 92 | + ), |
| 93 | + _ => ( |
| 94 | + "type parameters go unused in function definition", |
| 95 | + "consider removing the parameters", |
| 96 | + ), |
| 97 | + }; |
| 98 | + |
| 99 | + let source_map = self.cx.tcx.sess.source_map(); |
| 100 | + let span = if self.all_params_unused { |
| 101 | + self.generics.span.into() // Remove the entire list of generics |
| 102 | + } else { |
| 103 | + MultiSpan::from_spans( |
| 104 | + self.ty_params |
| 105 | + .iter() |
| 106 | + .map(|(def_id, &span)| { |
| 107 | + // Extend the span past any trait bounds, and include the comma at the end. |
| 108 | + let span_to_extend = self.bounds.get(def_id).copied().map_or(span, Span::shrink_to_hi); |
| 109 | + let comma_range = source_map.span_extend_to_next_char(span_to_extend, '>', false); |
| 110 | + let comma_span = source_map.span_through_char(comma_range, ','); |
| 111 | + span.with_hi(comma_span.hi()) |
| 112 | + }) |
| 113 | + .collect(), |
| 114 | + ) |
| 115 | + }; |
| 116 | + |
| 117 | + span_lint_and_help(self.cx, EXTRA_UNUSED_TYPE_PARAMETERS, span, msg, None, help); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> { |
| 122 | + type NestedFilter = nested_filter::OnlyBodies; |
| 123 | + |
| 124 | + fn visit_ty(&mut self, t: &'tcx Ty<'tcx>) { |
| 125 | + if let Some((def_id, _)) = t.peel_refs().as_generic_param() { |
| 126 | + if self.ty_params.remove(&def_id).is_some() { |
| 127 | + self.all_params_unused = false; |
| 128 | + } |
| 129 | + } else if let TyKind::OpaqueDef(id, _, _) = t.kind { |
| 130 | + // Explicitly walk OpaqueDef. Normally `walk_ty` would do the job, but it calls |
| 131 | + // `visit_nested_item`, which checks that `Self::NestedFilter::INTER` is set. We're |
| 132 | + // using `OnlyBodies`, so the check ends up failing and the type isn't fully walked. |
| 133 | + let item = self.nested_visit_map().item(id); |
| 134 | + walk_item(self, item); |
| 135 | + } else { |
| 136 | + walk_ty(self, t); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + fn visit_where_predicate(&mut self, predicate: &'tcx WherePredicate<'tcx>) { |
| 141 | + if let WherePredicate::BoundPredicate(predicate) = predicate { |
| 142 | + // Collect spans for bounds that appear in the list of generics (not in a where-clause) |
| 143 | + // for use in forming the help message |
| 144 | + if let Some((def_id, _)) = predicate.bounded_ty.peel_refs().as_generic_param() |
| 145 | + && let PredicateOrigin::GenericParam = predicate.origin |
| 146 | + { |
| 147 | + self.bounds.insert(def_id, predicate.span); |
| 148 | + } |
| 149 | + // Only walk the right-hand side of where-bounds |
| 150 | + for bound in predicate.bounds { |
| 151 | + walk_param_bound(self, bound); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + fn nested_visit_map(&mut self) -> Self::Map { |
| 157 | + self.cx.tcx.hir() |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters { |
| 162 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { |
| 163 | + if let ItemKind::Fn(_, generics, _) = item.kind { |
| 164 | + let mut walker = TypeWalker::new(cx, generics); |
| 165 | + walk_item(&mut walker, item); |
| 166 | + walker.emit_lint(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) { |
| 171 | + // Only lint on inherent methods, not trait methods. |
| 172 | + if let ImplItemKind::Fn(..) = item.kind && trait_ref_of_method(cx, item.owner_id.def_id).is_none() { |
| 173 | + let mut walker = TypeWalker::new(cx, item.generics); |
| 174 | + walk_impl_item(&mut walker, item); |
| 175 | + walker.emit_lint(); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments