]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/trait_bounds.rs
Fix some of the compile errors
[rust.git] / clippy_lints / src / trait_bounds.rs
1 use crate::utils::{in_macro, span_help_and_lint, SpanlessHash};
2 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array, impl_lint_pass};
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc::hir::*;
6
7 #[derive(Copy, Clone)]
8 pub struct TraitBounds;
9
10 declare_clippy_lint! {
11     pub TYPE_REPETITION_IN_BOUNDS,
12     complexity,
13     "Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
14 }
15
16 impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS]);
17
18 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds {
19     fn check_generics(&mut self, cx: &LateContext<'a, 'tcx>, gen: &'tcx Generics) {
20         if in_macro(gen.span) {
21             return;
22         }
23         let hash = | ty | -> u64 {
24             let mut hasher = SpanlessHash::new(cx, cx.tables);
25             hasher.hash_ty(ty);
26             hasher.finish()
27         };
28         let mut map = FxHashMap::default();
29         for bound in &gen.where_clause.predicates {
30             if let WherePredicate::BoundPredicate(ref p) = bound {
31                 let h = hash(&p.bounded_ty.node);
32                 if let Some(ref v) = map.insert(h, p.bounds) {
33                     let mut hint_string = format!("consider combining the bounds: `{:?}: ", p.bounded_ty);
34                     for &b in v.iter() {
35                         hint_string.push_str(&format!("{:?}, ", b));
36                     }
37                     for &b in p.bounds.iter() {
38                         hint_string.push_str(&format!("{:?}, ", b));
39                     }
40                     hint_string.truncate(hint_string.len() - 2);
41                     hint_string.push('`');
42                     span_help_and_lint(
43                         cx,
44                         TYPE_REPETITION_IN_BOUNDS,
45                         p.span,
46                         "this type has already been used as a bound predicate",
47                         &hint_string,
48                     );
49                 }
50             }
51         }
52     }
53 }