Update trait_bounds.rs

This commit is contained in:
John Kelly 2023-05-02 18:21:23 +01:00
parent cbd0135bd2
commit 122793b494

View File

@ -9,7 +9,7 @@ use rustc_data_structures::unhash::UnhashMap;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{
GenericArg, GenericBound, Generics, Item, ItemKind, MutTy, Node, Path, PathSegment, PredicateOrigin, QPath,
GenericArg, GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath,
TraitBoundModifier, TraitItem, TraitRef, Ty, TyKind, WherePredicate,
};
use rustc_lint::{LateContext, LateLintPass};
@ -169,59 +169,48 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
}
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
let TyKind::Ref(
..,
MutTy {
ty: Ty {
kind: TyKind::TraitObject(
bounds,
..
),
..
},
..
if_chain! {
if let TyKind::Ref(.., mut_ty) = &ty.kind;
if let TyKind::TraitObject(bounds, ..) = mut_ty.ty.kind;
if bounds.len() > 2;
then {
let mut bounds_span = bounds[0].span;
for bound in bounds.iter().skip(1) {
bounds_span = bounds_span.to(bound.span);
}
let mut seen_def_ids = FxHashSet::default();
let mut fixed_traits = Vec::new();
for bound in bounds.iter() {
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
let new_trait = seen_def_ids.insert(def_id);
if new_trait {
fixed_traits.push(bound);
}
}
if bounds.len() != fixed_traits.len() {
let fixed_trait_snippet = fixed_traits
.iter()
.filter_map(|b| snippet_opt(cx, b.span))
.collect::<Vec<_>>()
.join(" + ");
span_lint_and_sugg(
cx,
TRAIT_DUPLICATION_IN_BOUNDS,
bounds_span,
"this trait bound is already specified in trait declaration",
"try",
fixed_trait_snippet,
Applicability::MaybeIncorrect,
);
}
}
) = ty.kind else { return; };
if bounds.len() < 2 {
return;
}
let mut bounds_span = bounds[0].span;
for bound in bounds.iter().skip(1) {
bounds_span = bounds_span.to(bound.span);
}
let mut seen_def_ids = FxHashSet::default();
let mut fixed_traits = Vec::new();
for bound in bounds.iter() {
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
let new_trait = seen_def_ids.insert(def_id);
if new_trait {
fixed_traits.push(bound);
}
}
if bounds.len() != fixed_traits.len() {
let fixed_trait_snippet = fixed_traits
.iter()
.filter_map(|b| snippet_opt(cx, b.span))
.collect::<Vec<_>>()
.join(" + ");
span_lint_and_sugg(
cx,
TRAIT_DUPLICATION_IN_BOUNDS,
bounds_span,
"this trait bound is already specified in trait declaration",
"try",
fixed_trait_snippet,
Applicability::MaybeIncorrect,
);
}
}
}