]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/trait_bounds.rs
Rollup merge of #73870 - sexxi-goose:projection-ty, r=nikomatsakis
[rust.git] / clippy_lints / src / trait_bounds.rs
1 use crate::utils::{in_macro, snippet, snippet_with_applicability, span_lint_and_help, SpanlessHash};
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_errors::Applicability;
4 use rustc_hir::{GenericBound, Generics, WherePredicate};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_tool_lint, impl_lint_pass};
7
8 #[derive(Copy, Clone)]
9 pub struct TraitBounds;
10
11 declare_clippy_lint! {
12     /// **What it does:** This lint warns about unnecessary type repetitions in trait bounds
13     ///
14     /// **Why is this bad?** Repeating the type for every bound makes the code
15     /// less readable than combining the bounds
16     ///
17     /// **Example:**
18     /// ```rust
19     /// pub fn foo<T>(t: T) where T: Copy, T: Clone {}
20     /// ```
21     ///
22     /// Could be written as:
23     ///
24     /// ```rust
25     /// pub fn foo<T>(t: T) where T: Copy + Clone {}
26     /// ```
27     pub TYPE_REPETITION_IN_BOUNDS,
28     pedantic,
29     "Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
30 }
31
32 impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS]);
33
34 impl<'tcx> LateLintPass<'tcx> for TraitBounds {
35     fn check_generics(&mut self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
36         if in_macro(gen.span) {
37             return;
38         }
39         let hash = |ty| -> u64 {
40             let mut hasher = SpanlessHash::new(cx);
41             hasher.hash_ty(ty);
42             hasher.finish()
43         };
44         let mut map = FxHashMap::default();
45         let mut applicability = Applicability::MaybeIncorrect;
46         for bound in gen.where_clause.predicates {
47             if let WherePredicate::BoundPredicate(ref p) = bound {
48                 let h = hash(&p.bounded_ty);
49                 if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>()) {
50                     let mut hint_string = format!(
51                         "consider combining the bounds: `{}:",
52                         snippet(cx, p.bounded_ty.span, "_")
53                     );
54                     for b in v.iter() {
55                         if let GenericBound::Trait(ref poly_trait_ref, _) = b {
56                             let path = &poly_trait_ref.trait_ref.path;
57                             hint_string.push_str(&format!(
58                                 " {} +",
59                                 snippet_with_applicability(cx, path.span, "..", &mut applicability)
60                             ));
61                         }
62                     }
63                     for b in p.bounds.iter() {
64                         if let GenericBound::Trait(ref poly_trait_ref, _) = b {
65                             let path = &poly_trait_ref.trait_ref.path;
66                             hint_string.push_str(&format!(
67                                 " {} +",
68                                 snippet_with_applicability(cx, path.span, "..", &mut applicability)
69                             ));
70                         }
71                     }
72                     hint_string.truncate(hint_string.len() - 2);
73                     hint_string.push('`');
74                     span_lint_and_help(
75                         cx,
76                         TYPE_REPETITION_IN_BOUNDS,
77                         p.span,
78                         "this type has already been used as a bound predicate",
79                         None,
80                         &hint_string,
81                     );
82                 }
83             }
84         }
85     }
86 }