2020-03-27 09:34:29 -05:00
|
|
|
use crate::utils::{in_macro, snippet, snippet_with_applicability, span_lint_and_help, SpanlessHash};
|
2020-07-03 04:48:28 -05:00
|
|
|
use if_chain::if_chain;
|
2019-02-15 14:21:13 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-03-27 09:34:29 -05:00
|
|
|
use rustc_errors::Applicability;
|
2020-07-28 09:23:47 -05:00
|
|
|
use rustc_hir::{def::Res, GenericBound, Generics, ParamName, Path, QPath, TyKind, 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};
|
2020-07-28 09:23:47 -05:00
|
|
|
use rustc_span::Span;
|
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
|
|
|
///
|
2020-07-03 04:48:28 -05:00
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
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: _`"
|
|
|
|
}
|
|
|
|
|
2020-07-28 09:23:47 -05:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for cases where generics are being used and multiple
|
|
|
|
/// syntax specifications for trait bounds are used simultaneously.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Duplicate bounds makes the code
|
|
|
|
/// less readable than specifing them only once.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// fn func<T: Clone + Default>(arg: T) where T: Clone + Default {}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written as:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn func<T: Clone + Default>(arg: T) {}
|
|
|
|
/// ```
|
|
|
|
/// or
|
2020-08-04 13:23:14 -05:00
|
|
|
///
|
2020-07-28 09:23:47 -05:00
|
|
|
/// ```rust
|
|
|
|
/// fn func<T>(arg: T) where T: Clone + Default {}
|
|
|
|
/// ```
|
|
|
|
pub TRAIT_DUPLICATION_IN_BOUNDS,
|
|
|
|
pedantic,
|
2020-07-29 09:10:15 -05:00
|
|
|
"Check if the same trait bounds are specified twice during a function declaration"
|
2020-07-28 09:23:47 -05:00
|
|
|
}
|
|
|
|
|
2020-06-28 05:14:04 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct TraitBounds {
|
|
|
|
max_trait_bounds: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitBounds {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(max_trait_bounds: u64) -> Self {
|
|
|
|
Self { max_trait_bounds }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 09:23:47 -05:00
|
|
|
impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS, TRAIT_DUPLICATION_IN_BOUNDS]);
|
2019-02-15 14:21:13 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TraitBounds {
|
|
|
|
fn check_generics(&mut self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
|
2020-07-28 09:23:47 -05:00
|
|
|
self.check_type_repetition(cx, gen);
|
|
|
|
check_trait_bound_duplication(cx, gen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_trait_res_span_from_bound(bound: &GenericBound<'_>) -> Option<(Res, Span)> {
|
|
|
|
if let GenericBound::Trait(t, _) = bound {
|
|
|
|
Some((t.trait_ref.path.res, t.span))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitBounds {
|
|
|
|
fn check_type_repetition(self, cx: &LateContext<'_>, gen: &'_ 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 {
|
2020-06-25 21:55:23 -05:00
|
|
|
let mut hasher = SpanlessHash::new(cx);
|
2019-02-15 14:21:13 -06:00
|
|
|
hasher.hash_ty(ty);
|
|
|
|
hasher.finish()
|
|
|
|
};
|
|
|
|
let mut map = FxHashMap::default();
|
2020-03-27 09:34:29 -05:00
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
2019-12-29 22:02:10 -06:00
|
|
|
for bound in gen.where_clause.predicates {
|
2020-07-03 04:48:28 -05:00
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref p) = bound;
|
|
|
|
if p.bounds.len() as u64 <= self.max_trait_bounds;
|
|
|
|
if !in_macro(p.span);
|
2019-07-24 16:59:32 -05:00
|
|
|
let h = hash(&p.bounded_ty);
|
2020-07-03 04:48:28 -05:00
|
|
|
if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>());
|
|
|
|
|
|
|
|
then {
|
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;
|
2020-03-27 09:34:29 -05:00
|
|
|
hint_string.push_str(&format!(
|
|
|
|
" {} +",
|
|
|
|
snippet_with_applicability(cx, path.span, "..", &mut applicability)
|
|
|
|
));
|
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;
|
2020-03-27 09:34:29 -05:00
|
|
|
hint_string.push_str(&format!(
|
|
|
|
" {} +",
|
|
|
|
snippet_with_applicability(cx, path.span, "..", &mut applicability)
|
|
|
|
));
|
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",
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2019-02-15 14:21:13 -06:00
|
|
|
&hint_string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 09:23:47 -05:00
|
|
|
|
|
|
|
fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
|
2020-08-03 05:32:23 -05:00
|
|
|
if in_macro(gen.span) || gen.params.is_empty() || gen.where_clause.predicates.is_empty() {
|
2020-07-28 09:23:47 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut map = FxHashMap::default();
|
|
|
|
for param in gen.params {
|
|
|
|
if let ParamName::Plain(ref ident) = param.name {
|
|
|
|
let res = param
|
|
|
|
.bounds
|
|
|
|
.iter()
|
|
|
|
.filter_map(get_trait_res_span_from_bound)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
map.insert(*ident, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for predicate in gen.where_clause.predicates {
|
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
|
|
|
|
if !in_macro(bound_predicate.span);
|
2020-11-24 20:57:09 -06:00
|
|
|
if let TyKind::Path(QPath::Resolved(_, Path { ref segments, .. })) = bound_predicate.bounded_ty.kind;
|
2020-07-28 09:23:47 -05:00
|
|
|
if let Some(segment) = segments.first();
|
|
|
|
if let Some(trait_resolutions_direct) = map.get(&segment.ident);
|
|
|
|
then {
|
|
|
|
for (res_where, _) in bound_predicate.bounds.iter().filter_map(get_trait_res_span_from_bound) {
|
|
|
|
if let Some((_, span_direct)) = trait_resolutions_direct
|
|
|
|
.iter()
|
|
|
|
.find(|(res_direct, _)| *res_direct == res_where) {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
TRAIT_DUPLICATION_IN_BOUNDS,
|
|
|
|
*span_direct,
|
|
|
|
"this trait bound is already specified in the where clause",
|
|
|
|
None,
|
|
|
|
"consider removing this trait bound",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|