From 122793b4947c2691efcb2ab6450c251a25311aaf Mon Sep 17 00:00:00 2001 From: John Kelly Date: Tue, 2 May 2023 18:21:23 +0100 Subject: [PATCH] Update trait_bounds.rs --- clippy_lints/src/trait_bounds.rs | 95 ++++++++++++++------------------ 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/clippy_lints/src/trait_bounds.rs b/clippy_lints/src/trait_bounds.rs index ac61fb157b48..5acd44dccaf5 100644 --- a/clippy_lints/src/trait_bounds.rs +++ b/clippy_lints/src/trait_bounds.rs @@ -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::>() + .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::>() - .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, - ); } } }