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;
8 pub struct TraitBounds;
10 declare_clippy_lint! {
11 pub TYPE_REPETITION_IN_BOUNDS,
13 "Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
16 impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS]);
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) {
23 let hash = | ty | -> u64 {
24 let mut hasher = SpanlessHash::new(cx, cx.tables);
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);
35 hint_string.push_str(&format!("{:?}, ", b));
37 for &b in p.bounds.iter() {
38 hint_string.push_str(&format!("{:?}, ", b));
40 hint_string.truncate(hint_string.len() - 2);
41 hint_string.push('`');
44 TYPE_REPETITION_IN_BOUNDS,
46 "this type has already been used as a bound predicate",