Skip to content

Commit

Permalink
Support linting self trait bounds for repitition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Hsu committed May 29, 2022
1 parent 40d60cf commit 5999150
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions clippy_lints/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
check_bounds_or_where_duplication(cx, gen);
}

fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
// special handling for self trait bounds as these are not considered generics
// ie. trait Foo: Display {}
if let Item {
kind: ItemKind::Trait(_, _, _, bounds, ..),
..
} = item
{
rollup_traits(cx, bounds, "these bounds contain repeated elements");
}
}

fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) {
let mut self_bounds_map = FxHashMap::default();

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/repeated_where_clause_or_trait_bound.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait GoodSelfWhereClause {
Self: Clone + Copy;
}

trait BadSelfTraitBound: Clone + Clone + Clone {
trait BadSelfTraitBound: Clone {
fn f();
}

Expand Down
8 changes: 7 additions & 1 deletion tests/ui/repeated_where_clause_or_trait_bound.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ error: these where clauses contain repeated elements
LL | T: Clone + Clone + Clone + Copy,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone + Copy`

error: these bounds contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:40:26
|
LL | trait BadSelfTraitBound: Clone + Clone + Clone {
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone`

error: these where clauses contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:47:15
|
Expand All @@ -40,5 +46,5 @@ error: these bounds contain repeated elements
LL | fn bad_generic<T: GenericTrait<u64> + GenericTrait<u32> + GenericTrait<u64>>(arg0: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `GenericTrait<u32> + GenericTrait<u64>`

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

0 comments on commit 5999150

Please sign in to comment.