Skip to content

Commit

Permalink
trait_duplication_in_bounds Update description and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
dswij committed Jan 10, 2022
1 parent f690978 commit f4dc348
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
29 changes: 16 additions & 13 deletions clippy_lints/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
.filter_map(get_trait_res_span_from_bound)
.for_each(|(trait_item_res, span)| {
if self_bounds_set.get(&trait_item_res).is_some() {
emit_lint(cx, span);
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",
);
}
});
}
Expand Down Expand Up @@ -242,21 +249,17 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
if let Some((_, span_direct)) = trait_resolutions_direct
.iter()
.find(|(res_direct, _)| *res_direct == res_where) {
emit_lint(cx, *span_direct);
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",
);
}
}
}
}
}
}

fn emit_lint(cx: &LateContext<'_>, span: Span) {
span_lint_and_help(
cx,
TRAIT_DUPLICATION_IN_BOUNDS,
span,
"this trait bound is already specified in the where clause",
None,
"consider removing this trait bound",
);
}
8 changes: 8 additions & 0 deletions tests/ui/trait_duplication_in_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ trait U: Default {
}

trait ZZ: Default {
fn g();
fn h();
fn f()
where
Self: Default + Clone;
Expand All @@ -50,6 +52,12 @@ trait BadTrait: Default + Clone {
fn f()
where
Self: Default + Clone;
fn g()
where
Self: Default;
fn h()
where
Self: Copy;
}

#[derive(Default, Clone)]
Expand Down
24 changes: 16 additions & 8 deletions tests/ui/trait_duplication_in_bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,45 @@ LL | fn bad_foo<T: Clone + Default, Z: Copy>(arg0: T, arg1: Z)
|
= help: consider removing this trait bound

error: this trait bound is already specified in the where clause
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:34:15
|
LL | Self: Default;
| ^^^^^^^
|
= help: consider removing this trait bound

error: this trait bound is already specified in the where clause
--> $DIR/trait_duplication_in_bounds.rs:46:15
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:48:15
|
LL | Self: Default + Clone;
| ^^^^^^^
|
= help: consider removing this trait bound

error: this trait bound is already specified in the where clause
--> $DIR/trait_duplication_in_bounds.rs:52:15
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:54:15
|
LL | Self: Default + Clone;
| ^^^^^^^
|
= help: consider removing this trait bound

error: this trait bound is already specified in the where clause
--> $DIR/trait_duplication_in_bounds.rs:52:25
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:54:25
|
LL | Self: Default + Clone;
| ^^^^^
|
= help: consider removing this trait bound

error: aborting due to 6 previous errors
error: this trait bound is already specified in trait declaration
--> $DIR/trait_duplication_in_bounds.rs:57:15
|
LL | Self: Default;
| ^^^^^^^
|
= help: consider removing this trait bound

error: aborting due to 7 previous errors

0 comments on commit f4dc348

Please sign in to comment.