]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/trait_bounds.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / trait_bounds.rs
1 use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2 use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability};
3 use clippy_utils::{SpanlessEq, SpanlessHash};
4 use core::hash::{Hash, Hasher};
5 use if_chain::if_chain;
6 use itertools::Itertools;
7 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8 use rustc_data_structures::unhash::UnhashMap;
9 use rustc_errors::Applicability;
10 use rustc_hir::def::Res;
11 use rustc_hir::{
12 GenericArg, GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath,
13 TraitBoundModifier, TraitItem, TraitRef, Ty, TyKind, WherePredicate,
14 };
15 use rustc_lint::{LateContext, LateLintPass};
16 use rustc_session::{declare_tool_lint, impl_lint_pass};
17 use rustc_span::{BytePos, Span};
18 use std::collections::hash_map::Entry;
19
20 declare_clippy_lint! {
21 /// ### What it does
22 /// This lint warns about unnecessary type repetitions in trait bounds
23 ///
24 /// ### Why is this bad?
25 /// Repeating the type for every bound makes the code
26 /// less readable than combining the bounds
27 ///
28 /// ### Example
29 /// ```rust
30 /// pub fn foo<T>(t: T) where T: Copy, T: Clone {}
31 /// ```
32 ///
33 /// Use instead:
34 /// ```rust
35 /// pub fn foo<T>(t: T) where T: Copy + Clone {}
36 /// ```
37 #[clippy::version = "1.38.0"]
38 pub TYPE_REPETITION_IN_BOUNDS,
39 nursery,
40 "types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
41 }
42
43 declare_clippy_lint! {
44 /// ### What it does
45 /// Checks for cases where generics are being used and multiple
46 /// syntax specifications for trait bounds are used simultaneously.
47 ///
48 /// ### Why is this bad?
49 /// Duplicate bounds makes the code
50 /// less readable than specifying them only once.
51 ///
52 /// ### Example
53 /// ```rust
54 /// fn func<T: Clone + Default>(arg: T) where T: Clone + Default {}
55 /// ```
56 ///
57 /// Use instead:
58 /// ```rust
59 /// # mod hidden {
60 /// fn func<T: Clone + Default>(arg: T) {}
61 /// # }
62 ///
63 /// // or
64 ///
65 /// fn func<T>(arg: T) where T: Clone + Default {}
66 /// ```
67 ///
68 /// ```rust
69 /// fn foo<T: Default + Default>(bar: T) {}
70 /// ```
71 /// Use instead:
72 /// ```rust
73 /// fn foo<T: Default>(bar: T) {}
74 /// ```
75 ///
76 /// ```rust
77 /// fn foo<T>(bar: T) where T: Default + Default {}
78 /// ```
79 /// Use instead:
80 /// ```rust
81 /// fn foo<T>(bar: T) where T: Default {}
82 /// ```
83 #[clippy::version = "1.47.0"]
84 pub TRAIT_DUPLICATION_IN_BOUNDS,
85 nursery,
86 "check if the same trait bounds are specified more than once during a generic declaration"
87 }
88
89 #[derive(Copy, Clone)]
90 pub struct TraitBounds {
91 max_trait_bounds: u64,
92 }
93
94 impl TraitBounds {
95 #[must_use]
96 pub fn new(max_trait_bounds: u64) -> Self {
97 Self { max_trait_bounds }
98 }
99 }
100
101 impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS, TRAIT_DUPLICATION_IN_BOUNDS]);
102
103 impl<'tcx> LateLintPass<'tcx> for TraitBounds {
104 fn check_generics(&mut self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
105 self.check_type_repetition(cx, gen);
106 check_trait_bound_duplication(cx, gen);
107 }
108
109 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
110 // special handling for self trait bounds as these are not considered generics
111 // ie. trait Foo: Display {}
112 if let Item {
113 kind: ItemKind::Trait(_, _, _, bounds, ..),
114 ..
115 } = item
116 {
117 rollup_traits(cx, bounds, "these bounds contain repeated elements");
118 }
119 }
120
121 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) {
122 let mut self_bounds_map = FxHashMap::default();
123
124 for predicate in item.generics.predicates {
125 if_chain! {
126 if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
127 if bound_predicate.origin != PredicateOrigin::ImplTrait;
128 if !bound_predicate.span.from_expansion();
129 if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
130 if let Some(PathSegment {
131 res: Res::SelfTyParam { trait_: def_id }, ..
132 }) = segments.first();
133 if let Some(
134 Node::Item(
135 Item {
136 kind: ItemKind::Trait(_, _, _, self_bounds, _),
137 .. }
138 )
139 ) = cx.tcx.hir().get_if_local(*def_id);
140 then {
141 if self_bounds_map.is_empty() {
142 for bound in self_bounds.iter() {
143 let Some((self_res, self_segments, _)) = get_trait_info_from_bound(bound) else { continue };
144 self_bounds_map.insert(self_res, self_segments);
145 }
146 }
147
148 bound_predicate
149 .bounds
150 .iter()
151 .filter_map(get_trait_info_from_bound)
152 .for_each(|(trait_item_res, trait_item_segments, span)| {
153 if let Some(self_segments) = self_bounds_map.get(&trait_item_res) {
154 if SpanlessEq::new(cx).eq_path_segments(self_segments, trait_item_segments) {
155 span_lint_and_help(
156 cx,
157 TRAIT_DUPLICATION_IN_BOUNDS,
158 span,
159 "this trait bound is already specified in trait declaration",
160 None,
161 "consider removing this trait bound",
162 );
163 }
164 }
165 });
166 }
167 }
168 }
169 }
170 }
171
172 impl TraitBounds {
173 fn check_type_repetition<'tcx>(self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
174 struct SpanlessTy<'cx, 'tcx> {
175 ty: &'tcx Ty<'tcx>,
176 cx: &'cx LateContext<'tcx>,
177 }
178 impl PartialEq for SpanlessTy<'_, '_> {
179 fn eq(&self, other: &Self) -> bool {
180 let mut eq = SpanlessEq::new(self.cx);
181 eq.inter_expr().eq_ty(self.ty, other.ty)
182 }
183 }
184 impl Hash for SpanlessTy<'_, '_> {
185 fn hash<H: Hasher>(&self, h: &mut H) {
186 let mut t = SpanlessHash::new(self.cx);
187 t.hash_ty(self.ty);
188 h.write_u64(t.finish());
189 }
190 }
191 impl Eq for SpanlessTy<'_, '_> {}
192
193 if gen.span.from_expansion() {
194 return;
195 }
196 let mut map: UnhashMap<SpanlessTy<'_, '_>, Vec<&GenericBound<'_>>> = UnhashMap::default();
197 let mut applicability = Applicability::MaybeIncorrect;
198 for bound in gen.predicates {
199 if_chain! {
200 if let WherePredicate::BoundPredicate(ref p) = bound;
201 if p.origin != PredicateOrigin::ImplTrait;
202 if p.bounds.len() as u64 <= self.max_trait_bounds;
203 if !p.span.from_expansion();
204 if let Some(ref v) = map.insert(
205 SpanlessTy { ty: p.bounded_ty, cx },
206 p.bounds.iter().collect::<Vec<_>>()
207 );
208
209 then {
210 let trait_bounds = v
211 .iter()
212 .copied()
213 .chain(p.bounds.iter())
214 .filter_map(get_trait_info_from_bound)
215 .map(|(_, _, span)| snippet_with_applicability(cx, span, "..", &mut applicability))
216 .join(" + ");
217 let hint_string = format!(
218 "consider combining the bounds: `{}: {trait_bounds}`",
219 snippet(cx, p.bounded_ty.span, "_"),
220 );
221 span_lint_and_help(
222 cx,
223 TYPE_REPETITION_IN_BOUNDS,
224 p.span,
225 "this type has already been used as a bound predicate",
226 None,
227 &hint_string,
228 );
229 }
230 }
231 }
232 }
233 }
234
235 fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
236 if gen.span.from_expansion() {
237 return;
238 }
239
240 // Explanation:
241 // fn bad_foo<T: Clone + Default, Z: Copy>(arg0: T, arg1: Z)
242 // where T: Clone + Default, { unimplemented!(); }
243 // ^^^^^^^^^^^^^^^^^^
244 // |
245 // collects each of these where clauses into a set keyed by generic name and comparable trait
246 // eg. (T, Clone)
247 let where_predicates = gen
248 .predicates
249 .iter()
250 .filter_map(|pred| {
251 if_chain! {
252 if pred.in_where_clause();
253 if let WherePredicate::BoundPredicate(bound_predicate) = pred;
254 if let TyKind::Path(QPath::Resolved(_, path)) = bound_predicate.bounded_ty.kind;
255 then {
256 return Some(
257 rollup_traits(cx, bound_predicate.bounds, "these where clauses contain repeated elements")
258 .into_iter().map(|(trait_ref, _)| (path.res, trait_ref)))
259 }
260 }
261 None
262 })
263 .flatten()
264 .collect::<FxHashSet<_>>();
265
266 // Explanation:
267 // fn bad_foo<T: Clone + Default, Z: Copy>(arg0: T, arg1: Z) ...
268 // ^^^^^^^^^^^^^^^^^^ ^^^^^^^
269 // |
270 // compare trait bounds keyed by generic name and comparable trait to collected where
271 // predicates eg. (T, Clone)
272 for predicate in gen.predicates.iter().filter(|pred| !pred.in_where_clause()) {
273 if_chain! {
274 if let WherePredicate::BoundPredicate(bound_predicate) = predicate;
275 if bound_predicate.origin != PredicateOrigin::ImplTrait;
276 if !bound_predicate.span.from_expansion();
277 if let TyKind::Path(QPath::Resolved(_, path)) = bound_predicate.bounded_ty.kind;
278 then {
279 let traits = rollup_traits(cx, bound_predicate.bounds, "these bounds contain repeated elements");
280 for (trait_ref, span) in traits {
281 let key = (path.res, trait_ref);
282 if where_predicates.contains(&key) {
283 span_lint_and_help(
284 cx,
285 TRAIT_DUPLICATION_IN_BOUNDS,
286 span,
287 "this trait bound is already specified in the where clause",
288 None,
289 "consider removing this trait bound",
290 );
291 }
292 }
293 }
294 }
295 }
296 }
297
298 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
299 struct ComparableTraitRef(Res, Vec<Res>);
300 impl Default for ComparableTraitRef {
301 fn default() -> Self {
302 Self(Res::Err, Vec::new())
303 }
304 }
305
306 fn get_trait_info_from_bound<'a>(bound: &'a GenericBound<'_>) -> Option<(Res, &'a [PathSegment<'a>], Span)> {
307 if let GenericBound::Trait(t, tbm) = bound {
308 let trait_path = t.trait_ref.path;
309 let trait_span = {
310 let path_span = trait_path.span;
311 if let TraitBoundModifier::Maybe = tbm {
312 path_span.with_lo(path_span.lo() - BytePos(1)) // include the `?`
313 } else {
314 path_span
315 }
316 };
317 Some((trait_path.res, trait_path.segments, trait_span))
318 } else {
319 None
320 }
321 }
322
323 // FIXME: ComparableTraitRef does not support nested bounds needed for associated_type_bounds
324 fn into_comparable_trait_ref(trait_ref: &TraitRef<'_>) -> ComparableTraitRef {
325 ComparableTraitRef(
326 trait_ref.path.res,
327 trait_ref
328 .path
329 .segments
330 .iter()
331 .filter_map(|segment| {
332 // get trait bound type arguments
333 Some(segment.args?.args.iter().filter_map(|arg| {
334 if_chain! {
335 if let GenericArg::Type(ty) = arg;
336 if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind;
337 then { return Some(path.res) }
338 }
339 None
340 }))
341 })
342 .flatten()
343 .collect(),
344 )
345 }
346
347 fn rollup_traits(cx: &LateContext<'_>, bounds: &[GenericBound<'_>], msg: &str) -> Vec<(ComparableTraitRef, Span)> {
348 let mut map = FxHashMap::default();
349 let mut repeated_res = false;
350
351 let only_comparable_trait_refs = |bound: &GenericBound<'_>| {
352 if let GenericBound::Trait(t, _) = bound {
353 Some((into_comparable_trait_ref(&t.trait_ref), t.span))
354 } else {
355 None
356 }
357 };
358
359 let mut i = 0usize;
360 for bound in bounds.iter().filter_map(only_comparable_trait_refs) {
361 let (comparable_bound, span_direct) = bound;
362 match map.entry(comparable_bound) {
363 Entry::Occupied(_) => repeated_res = true,
364 Entry::Vacant(e) => {
365 e.insert((span_direct, i));
366 i += 1;
367 },
368 }
369 }
370
371 // Put bounds in source order
372 let mut comparable_bounds = vec![Default::default(); map.len()];
373 for (k, (v, i)) in map {
374 comparable_bounds[i] = (k, v);
375 }
376
377 if_chain! {
378 if repeated_res;
379 if let [first_trait, .., last_trait] = bounds;
380 then {
381 let all_trait_span = first_trait.span().to(last_trait.span());
382
383 let traits = comparable_bounds.iter()
384 .filter_map(|&(_, span)| snippet_opt(cx, span))
385 .collect::<Vec<_>>();
386 let traits = traits.join(" + ");
387
388 span_lint_and_sugg(
389 cx,
390 TRAIT_DUPLICATION_IN_BOUNDS,
391 all_trait_span,
392 msg,
393 "try",
394 traits,
395 Applicability::MachineApplicable
396 );
397 }
398 }
399
400 comparable_bounds
401 }