Skip to content

Commit 3cb412a

Browse files
Allen Hsualdhsu
authored andcommitted
Support linting self trait bounds for repitition.
1 parent 360418f commit 3cb412a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
119119
check_bounds_or_where_duplication(cx, gen);
120120
}
121121

122+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
123+
// special handling for self trait bounds as these are not considered generics
124+
// ie. trait Foo: Display {}
125+
if let Item {
126+
kind: ItemKind::Trait(_, _, _, bounds, ..),
127+
..
128+
} = item
129+
{
130+
rollup_traits(cx, bounds, "these bounds contain repeated elements");
131+
}
132+
}
133+
122134
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) {
123135
let mut self_bounds_map = FxHashMap::default();
124136

tests/ui/repeated_where_clause_or_trait_bound.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait GoodSelfWhereClause {
3737
Self: Clone + Copy;
3838
}
3939

40-
trait BadSelfTraitBound: Clone + Clone + Clone {
40+
trait BadSelfTraitBound: Clone {
4141
fn f();
4242
}
4343

tests/ui/repeated_where_clause_or_trait_bound.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ error: these where clauses contain repeated elements
1616
LL | T: Clone + Clone + Clone + Copy,
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone + Copy`
1818

19+
error: these bounds contain repeated elements
20+
--> $DIR/repeated_where_clause_or_trait_bound.rs:40:26
21+
|
22+
LL | trait BadSelfTraitBound: Clone + Clone + Clone {
23+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone`
24+
1925
error: these where clauses contain repeated elements
2026
--> $DIR/repeated_where_clause_or_trait_bound.rs:47:15
2127
|
@@ -40,5 +46,5 @@ error: these bounds contain repeated elements
4046
LL | fn bad_generic<T: GenericTrait<u64> + GenericTrait<u32> + GenericTrait<u64>>(arg0: T) {
4147
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `GenericTrait<u32> + GenericTrait<u64>`
4248

43-
error: aborting due to 6 previous errors
49+
error: aborting due to 7 previous errors
4450

0 commit comments

Comments
 (0)