2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
|
|
|
use clippy_utils::source::{snippet, snippet_with_applicability};
|
2022-01-13 12:18:19 +00:00
|
|
|
use clippy_utils::{SpanlessEq, SpanlessHash};
|
|
|
|
use core::hash::{Hash, Hasher};
|
2020-07-14 12:59:59 +00:00
|
|
|
use if_chain::if_chain;
|
2022-07-07 09:59:55 +00:00
|
|
|
use itertools::Itertools;
|
2022-01-27 14:12:45 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-06-03 06:41:37 +00:00
|
|
|
use rustc_data_structures::unhash::UnhashMap;
|
2020-03-27 14:34:29 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-13 12:18:19 +00:00
|
|
|
use rustc_hir::def::Res;
|
|
|
|
use rustc_hir::{
|
2022-07-07 10:24:17 +00:00
|
|
|
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath, TraitBoundModifier,
|
|
|
|
TraitItem, Ty, TyKind, WherePredicate,
|
2022-01-13 12:18:19 +00:00
|
|
|
};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2022-07-07 10:24:17 +00:00
|
|
|
use rustc_span::{BytePos, Span};
|
2019-02-15 20:21:13 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// This lint warns about unnecessary type repetitions in trait bounds
|
2019-07-27 11:06:25 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Repeating the type for every bound makes the code
|
2019-07-27 20:58:29 +00:00
|
|
|
/// less readable than combining the bounds
|
2019-07-27 11:06:25 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// pub fn foo<T>(t: T) where T: Copy, T: Clone {}
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2022-06-05 19:24:41 +00:00
|
|
|
/// Use instead:
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// pub fn foo<T>(t: T) where T: Copy + Clone {}
|
2019-07-27 11:06:25 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.38.0"]
|
2019-02-15 20:21:13 +00:00
|
|
|
pub TYPE_REPETITION_IN_BOUNDS,
|
2022-05-05 12:32:31 +00:00
|
|
|
nursery,
|
2019-02-15 20:21:13 +00:00
|
|
|
"Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for cases where generics are being used and multiple
|
2020-08-11 13:43:21 +00:00
|
|
|
/// syntax specifications for trait bounds are used simultaneously.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Duplicate bounds makes the code
|
2022-03-16 12:12:30 +00:00
|
|
|
/// less readable than specifying them only once.
|
2020-08-11 13:43:21 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2020-08-11 13:43:21 +00:00
|
|
|
/// ```rust
|
|
|
|
/// fn func<T: Clone + Default>(arg: T) where T: Clone + Default {}
|
|
|
|
/// ```
|
|
|
|
///
|
2022-06-01 22:36:02 +00:00
|
|
|
/// Use instead:
|
2020-08-11 13:43:21 +00:00
|
|
|
/// ```rust
|
2022-06-01 22:36:02 +00:00
|
|
|
/// # mod hidden {
|
2020-08-11 13:43:21 +00:00
|
|
|
/// fn func<T: Clone + Default>(arg: T) {}
|
2022-06-01 22:36:02 +00:00
|
|
|
/// # }
|
|
|
|
///
|
|
|
|
/// // or
|
2020-08-11 13:43:21 +00:00
|
|
|
///
|
|
|
|
/// fn func<T>(arg: T) where T: Clone + Default {}
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.47.0"]
|
2020-08-11 13:43:21 +00:00
|
|
|
pub TRAIT_DUPLICATION_IN_BOUNDS,
|
2022-05-05 12:32:31 +00:00
|
|
|
nursery,
|
2020-08-11 13:43:21 +00:00
|
|
|
"Check if the same trait bounds are specified twice during a function declaration"
|
|
|
|
}
|
|
|
|
|
2020-07-14 12:59:59 +00: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-08-11 13:43:21 +00:00
|
|
|
impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS, TRAIT_DUPLICATION_IN_BOUNDS]);
|
2019-02-15 20:21:13 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TraitBounds {
|
|
|
|
fn check_generics(&mut self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
|
2020-08-11 13:43:21 +00:00
|
|
|
self.check_type_repetition(cx, gen);
|
|
|
|
check_trait_bound_duplication(cx, gen);
|
|
|
|
}
|
2022-01-13 12:18:19 +00:00
|
|
|
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) {
|
2022-01-27 14:12:45 +00:00
|
|
|
let mut self_bounds_map = FxHashMap::default();
|
2022-01-13 12:18:19 +00:00
|
|
|
|
2022-02-05 14:48:02 +00:00
|
|
|
for predicate in item.generics.predicates {
|
2022-01-13 12:18:19 +00:00
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
|
2022-05-05 14:50:11 +00:00
|
|
|
if bound_predicate.origin != PredicateOrigin::ImplTrait;
|
2022-01-13 12:18:19 +00:00
|
|
|
if !bound_predicate.span.from_expansion();
|
|
|
|
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
|
2022-02-26 13:26:21 +00:00
|
|
|
if let Some(PathSegment {
|
|
|
|
res: Some(Res::SelfTy{ trait_: Some(def_id), alias_to: _ }), ..
|
|
|
|
}) = segments.first();
|
2022-01-13 12:18:19 +00:00
|
|
|
if let Some(
|
|
|
|
Node::Item(
|
|
|
|
Item {
|
|
|
|
kind: ItemKind::Trait(_, _, _, self_bounds, _),
|
|
|
|
.. }
|
|
|
|
)
|
|
|
|
) = cx.tcx.hir().get_if_local(*def_id);
|
|
|
|
then {
|
2022-01-27 14:12:45 +00:00
|
|
|
if self_bounds_map.is_empty() {
|
2022-01-13 12:18:19 +00:00
|
|
|
for bound in self_bounds.iter() {
|
2022-01-27 14:12:45 +00:00
|
|
|
let Some((self_res, self_segments, _)) = get_trait_info_from_bound(bound) else { continue };
|
|
|
|
self_bounds_map.insert(self_res, self_segments);
|
2022-01-13 12:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bound_predicate
|
|
|
|
.bounds
|
|
|
|
.iter()
|
2022-01-27 14:12:45 +00:00
|
|
|
.filter_map(get_trait_info_from_bound)
|
|
|
|
.for_each(|(trait_item_res, trait_item_segments, span)| {
|
|
|
|
if let Some(self_segments) = self_bounds_map.get(&trait_item_res) {
|
|
|
|
if SpanlessEq::new(cx).eq_path_segments(self_segments, trait_item_segments) {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
TRAIT_DUPLICATION_IN_BOUNDS,
|
|
|
|
span,
|
|
|
|
"this trait bound is already specified in trait declaration",
|
|
|
|
None,
|
|
|
|
"consider removing this trait bound",
|
|
|
|
);
|
|
|
|
}
|
2022-01-13 12:18:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 13:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TraitBounds {
|
2022-01-13 12:18:19 +00:00
|
|
|
fn check_type_repetition<'tcx>(self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
|
|
|
|
struct SpanlessTy<'cx, 'tcx> {
|
|
|
|
ty: &'tcx Ty<'tcx>,
|
|
|
|
cx: &'cx LateContext<'tcx>,
|
|
|
|
}
|
|
|
|
impl PartialEq for SpanlessTy<'_, '_> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
let mut eq = SpanlessEq::new(self.cx);
|
|
|
|
eq.inter_expr().eq_ty(self.ty, other.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Hash for SpanlessTy<'_, '_> {
|
|
|
|
fn hash<H: Hasher>(&self, h: &mut H) {
|
|
|
|
let mut t = SpanlessHash::new(self.cx);
|
|
|
|
t.hash_ty(self.ty);
|
|
|
|
h.write_u64(t.finish());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Eq for SpanlessTy<'_, '_> {}
|
|
|
|
|
2021-12-06 11:33:31 +00:00
|
|
|
if gen.span.from_expansion() {
|
2019-02-15 20:21:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-13 12:18:19 +00:00
|
|
|
let mut map: UnhashMap<SpanlessTy<'_, '_>, Vec<&GenericBound<'_>>> = UnhashMap::default();
|
2020-03-27 14:34:29 +00:00
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
2022-02-05 14:48:02 +00:00
|
|
|
for bound in gen.predicates {
|
2020-07-14 12:59:59 +00:00
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref p) = bound;
|
2022-05-05 14:50:11 +00:00
|
|
|
if p.origin != PredicateOrigin::ImplTrait;
|
2020-07-14 12:59:59 +00:00
|
|
|
if p.bounds.len() as u64 <= self.max_trait_bounds;
|
2021-12-06 11:33:31 +00:00
|
|
|
if !p.span.from_expansion();
|
2022-01-13 12:18:19 +00:00
|
|
|
if let Some(ref v) = map.insert(
|
|
|
|
SpanlessTy { ty: p.bounded_ty, cx },
|
|
|
|
p.bounds.iter().collect::<Vec<_>>()
|
|
|
|
);
|
2020-07-14 12:59:59 +00:00
|
|
|
|
|
|
|
then {
|
2022-07-07 09:59:55 +00:00
|
|
|
let trait_bounds = v
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.chain(p.bounds.iter())
|
|
|
|
.filter_map(get_trait_info_from_bound)
|
|
|
|
.map(|(_, _, span)| snippet_with_applicability(cx, span, "..", &mut applicability))
|
|
|
|
.join(" + ");
|
|
|
|
let hint_string = format!(
|
|
|
|
"consider combining the bounds: `{}: {}`",
|
|
|
|
snippet(cx, p.bounded_ty.span, "_"),
|
|
|
|
trait_bounds,
|
2019-07-27 20:58:29 +00:00
|
|
|
);
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-02-15 20:21:13 +00:00
|
|
|
cx,
|
|
|
|
TYPE_REPETITION_IN_BOUNDS,
|
|
|
|
p.span,
|
|
|
|
"this type has already been used as a bound predicate",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2019-02-15 20:21:13 +00:00
|
|
|
&hint_string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 13:43:21 +00:00
|
|
|
|
|
|
|
fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
|
2022-02-05 14:48:02 +00:00
|
|
|
if gen.span.from_expansion() || gen.params.is_empty() || gen.predicates.is_empty() {
|
2020-08-11 13:43:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:58:30 +00:00
|
|
|
let mut map = FxHashMap::<_, Vec<_>>::default();
|
2022-02-05 14:48:02 +00:00
|
|
|
for predicate in gen.predicates {
|
2020-08-11 13:43:21 +00:00
|
|
|
if_chain! {
|
|
|
|
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate;
|
2022-05-05 14:50:11 +00:00
|
|
|
if bound_predicate.origin != PredicateOrigin::ImplTrait;
|
2021-12-06 11:33:31 +00:00
|
|
|
if !bound_predicate.span.from_expansion();
|
2021-04-08 15:50:13 +00:00
|
|
|
if let TyKind::Path(QPath::Resolved(_, Path { segments, .. })) = bound_predicate.bounded_ty.kind;
|
2020-08-11 13:43:21 +00:00
|
|
|
if let Some(segment) = segments.first();
|
|
|
|
then {
|
2022-02-07 21:58:30 +00:00
|
|
|
for (res_where, _, span_where) in bound_predicate.bounds.iter().filter_map(get_trait_info_from_bound) {
|
|
|
|
let trait_resolutions_direct = map.entry(segment.ident).or_default();
|
|
|
|
if let Some((_, span_direct)) = trait_resolutions_direct
|
2020-08-11 13:43:21 +00:00
|
|
|
.iter()
|
2022-02-07 21:58:30 +00:00
|
|
|
.find(|(res_direct, _)| *res_direct == res_where) {
|
2020-08-11 13:43:21 +00:00
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
2022-02-07 21:58:30 +00:00
|
|
|
else {
|
2022-05-05 14:12:52 +00:00
|
|
|
trait_resolutions_direct.push((res_where, span_where));
|
2022-02-07 21:58:30 +00:00
|
|
|
}
|
2020-08-11 13:43:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:12:45 +00:00
|
|
|
|
|
|
|
fn get_trait_info_from_bound<'a>(bound: &'a GenericBound<'_>) -> Option<(Res, &'a [PathSegment<'a>], Span)> {
|
2022-07-07 10:24:17 +00:00
|
|
|
if let GenericBound::Trait(t, tbm) = bound {
|
|
|
|
let trait_path = t.trait_ref.path;
|
|
|
|
let trait_span = {
|
|
|
|
let path_span = trait_path.span;
|
|
|
|
if let TraitBoundModifier::Maybe = tbm {
|
|
|
|
path_span.with_lo(path_span.lo() - BytePos(1)) // include the `?`
|
|
|
|
} else {
|
|
|
|
path_span
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some((trait_path.res, trait_path.segments, trait_span))
|
2022-01-27 14:12:45 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|