2020-01-26 19:56:22 -06:00
|
|
|
use crate::utils::{in_macro, snippet, span_lint_and_help, SpanlessHash};
|
2019-02-15 14:21:13 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{GenericBound, Generics, WherePredicate};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2019-02-15 14:21:13 -06:00
|
|
|
|
2019-07-24 16:27:12 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct TraitBounds;
|
|
|
|
|
2019-02-15 14:21:13 -06:00
|
|
|
declare_clippy_lint! {
|
2019-07-27 06:06:25 -05:00
|
|
|
/// **What it does:** This lint warns about unnecessary type repetitions in trait bounds
|
|
|
|
///
|
2019-07-27 15:58:29 -05:00
|
|
|
/// **Why is this bad?** Repeating the type for every bound makes the code
|
|
|
|
/// less readable than combining the bounds
|
2019-07-27 06:06:25 -05:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-02 01:13:54 -05:00
|
|
|
/// pub fn foo<T>(t: T) where T: Copy, T: Clone {}
|
2019-07-27 06:06:25 -05:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written as:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-02 01:13:54 -05:00
|
|
|
/// pub fn foo<T>(t: T) where T: Copy + Clone {}
|
2019-07-27 06:06:25 -05:00
|
|
|
/// ```
|
2019-02-15 14:21:13 -06:00
|
|
|
pub TYPE_REPETITION_IN_BOUNDS,
|
2019-08-17 05:45:05 -05:00
|
|
|
pedantic,
|
2019-02-15 14:21:13 -06:00
|
|
|
"Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
|
|
|
|
}
|
|
|
|
|
2019-07-24 16:27:12 -05:00
|
|
|
impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS]);
|
2019-02-15 14:21:13 -06:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds {
|
2019-12-29 22:02:10 -06:00
|
|
|
fn check_generics(&mut self, cx: &LateContext<'a, 'tcx>, gen: &'tcx Generics<'_>) {
|
2019-02-15 14:21:13 -06:00
|
|
|
if in_macro(gen.span) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-27 15:58:29 -05:00
|
|
|
let hash = |ty| -> u64 {
|
2019-02-15 14:21:13 -06:00
|
|
|
let mut hasher = SpanlessHash::new(cx, cx.tables);
|
|
|
|
hasher.hash_ty(ty);
|
|
|
|
hasher.finish()
|
|
|
|
};
|
|
|
|
let mut map = FxHashMap::default();
|
2019-12-29 22:02:10 -06:00
|
|
|
for bound in gen.where_clause.predicates {
|
2019-02-15 14:21:13 -06:00
|
|
|
if let WherePredicate::BoundPredicate(ref p) = bound {
|
2019-07-24 16:59:32 -05:00
|
|
|
let h = hash(&p.bounded_ty);
|
2019-07-26 10:46:47 -05:00
|
|
|
if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>()) {
|
2019-07-27 15:58:29 -05:00
|
|
|
let mut hint_string = format!(
|
|
|
|
"consider combining the bounds: `{}:",
|
|
|
|
snippet(cx, p.bounded_ty.span, "_")
|
|
|
|
);
|
2019-07-24 16:59:32 -05:00
|
|
|
for b in v.iter() {
|
2019-07-27 05:51:27 -05:00
|
|
|
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
|
|
|
|
let path = &poly_trait_ref.trait_ref.path;
|
2019-07-27 15:58:29 -05:00
|
|
|
hint_string.push_str(&format!(" {} +", path));
|
2019-07-27 05:51:27 -05:00
|
|
|
}
|
2019-02-15 14:21:13 -06:00
|
|
|
}
|
2019-07-24 16:59:32 -05:00
|
|
|
for b in p.bounds.iter() {
|
2019-07-27 05:51:27 -05:00
|
|
|
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
|
|
|
|
let path = &poly_trait_ref.trait_ref.path;
|
2019-07-27 15:58:29 -05:00
|
|
|
hint_string.push_str(&format!(" {} +", path));
|
2019-07-27 05:51:27 -05:00
|
|
|
}
|
2019-02-15 14:21:13 -06:00
|
|
|
}
|
|
|
|
hint_string.truncate(hint_string.len() - 2);
|
|
|
|
hint_string.push('`');
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-02-15 14:21:13 -06:00
|
|
|
cx,
|
|
|
|
TYPE_REPETITION_IN_BOUNDS,
|
|
|
|
p.span,
|
|
|
|
"this type has already been used as a bound predicate",
|
|
|
|
&hint_string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|