Skip to content

Commit b9788fe

Browse files
committed
Working
1 parent 8db21e9 commit b9788fe

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -187,37 +187,42 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
187187
return;
188188
}
189189

190-
let mut bounds_span = Span::default();
190+
let mut bounds_span = bounds[0].span;
191191

192-
for bound in bounds.iter() {
192+
for bound in bounds.iter().skip(1) {
193193
bounds_span = bounds_span.to(bound.span);
194194
}
195195

196196
let mut seen_def_ids = FxHashSet::default();
197-
198-
let traits = bounds
199-
.iter()
200-
.filter_map(|b| snippet_opt(cx, b.span))
201-
.collect::<Vec<_>>();
202-
let traits = traits.join(" + ");
197+
let mut fixed_traits = Vec::new();
203198

204199
for bound in bounds.iter() {
205200
let Some(def_id) = bound.trait_ref.trait_def_id() else { continue; };
206201

207-
let already_seen = !seen_def_ids.insert(def_id);
208-
209-
if already_seen {
210-
span_lint_and_sugg(
211-
cx,
212-
TRAIT_DUPLICATION_IN_BOUNDS,
213-
bounds_span,
214-
"this trait bound is already specified in trait declaration",
215-
"consider removing this trait bound",
216-
traits.clone(),
217-
Applicability::MaybeIncorrect,
218-
);
202+
let new_trait = seen_def_ids.insert(def_id);
203+
204+
if new_trait {
205+
fixed_traits.push(bound);
219206
}
220207
}
208+
209+
let fixed_trait_snippet = fixed_traits
210+
.iter()
211+
.filter_map(|b| snippet_opt(cx, b.span))
212+
.collect::<Vec<_>>()
213+
.join(" + ");
214+
215+
if bounds.len() != fixed_traits.len() {
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+
}
221226
}
222227
}
223228

tests/ui/trait_duplication_in_bounds.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ LL | fn qualified_path<T: std::clone::Clone + Clone + foo::Clone>(arg0: T) {
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::clone::Clone + foo::Clone`
5454

5555
error: this trait bound is already specified in trait declaration
56-
--> $DIR/trait_duplication_in_bounds.rs:118:46
56+
--> $DIR/trait_duplication_in_bounds.rs:118:33
5757
|
5858
LL | fn bad_trait_object(arg0: &(dyn Any + Send + Send)) {
59-
| ^^^^ help: consider removing this trait bound
60-
|
59+
| ^^^^^^^^^^^^^^^^^ help: try: `Any + Send`
6160

6261
error: aborting due to 9 previous errors
6362

0 commit comments

Comments
 (0)