Skip to content

Commit 122793b

Browse files
committed
Update trait_bounds.rs
1 parent cbd0135 commit 122793b

File tree

1 file changed

+35
-46
lines changed

1 file changed

+35
-46
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::unhash::UnhashMap;
99
use rustc_errors::Applicability;
1010
use rustc_hir::def::Res;
1111
use rustc_hir::{
12-
GenericArg, GenericBound, Generics, Item, ItemKind, MutTy, Node, Path, PathSegment, PredicateOrigin, QPath,
12+
GenericArg, GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath,
1313
TraitBoundModifier, TraitItem, TraitRef, Ty, TyKind, WherePredicate,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
@@ -169,60 +169,49 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
169169
}
170170

171171
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
172-
let TyKind::Ref(
173-
..,
174-
MutTy {
175-
ty: Ty {
176-
kind: TyKind::TraitObject(
177-
bounds,
178-
..
179-
),
180-
..
181-
},
182-
..
183-
}
184-
) = ty.kind else { return; };
172+
if_chain! {
173+
if let TyKind::Ref(.., mut_ty) = &ty.kind;
174+
if let TyKind::TraitObject(bounds, ..) = mut_ty.ty.kind;
175+
if bounds.len() > 2;
176+
then {
177+
let mut bounds_span = bounds[0].span;
185178

186-
if bounds.len() < 2 {
187-
return;
188-
}
179+
for bound in bounds.iter().skip(1) {
180+
bounds_span = bounds_span.to(bound.span);
181+
}
189182

190-
let mut bounds_span = bounds[0].span;
183+
let mut seen_def_ids = FxHashSet::default();
184+
let mut fixed_traits = Vec::new();
191185

192-
for bound in bounds.iter().skip(1) {
193-
bounds_span = bounds_span.to(bound.span);
194-
}
186+
for bound in bounds.iter() {
187+
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
195188

196-
let mut seen_def_ids = FxHashSet::default();
197-
let mut fixed_traits = Vec::new();
189+
let new_trait = seen_def_ids.insert(def_id);
198190

199-
for bound in bounds.iter() {
200-
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
191+
if new_trait {
192+
fixed_traits.push(bound);
193+
}
194+
}
201195

202-
let new_trait = seen_def_ids.insert(def_id);
196+
if bounds.len() != fixed_traits.len() {
197+
let fixed_trait_snippet = fixed_traits
198+
.iter()
199+
.filter_map(|b| snippet_opt(cx, b.span))
200+
.collect::<Vec<_>>()
201+
.join(" + ");
203202

204-
if new_trait {
205-
fixed_traits.push(bound);
203+
span_lint_and_sugg(
204+
cx,
205+
TRAIT_DUPLICATION_IN_BOUNDS,
206+
bounds_span,
207+
"this trait bound is already specified in trait declaration",
208+
"try",
209+
fixed_trait_snippet,
210+
Applicability::MaybeIncorrect,
211+
);
212+
}
206213
}
207214
}
208-
209-
if bounds.len() != fixed_traits.len() {
210-
let fixed_trait_snippet = fixed_traits
211-
.iter()
212-
.filter_map(|b| snippet_opt(cx, b.span))
213-
.collect::<Vec<_>>()
214-
.join(" + ");
215-
216-
span_lint_and_sugg(
217-
cx,
218-
TRAIT_DUPLICATION_IN_BOUNDS,
219-
bounds_span,
220-
"this trait bound is already specified in trait declaration",
221-
"try",
222-
fixed_trait_snippet,
223-
Applicability::MaybeIncorrect,
224-
);
225-
}
226215
}
227216
}
228217

0 commit comments

Comments
 (0)