Skip to content

Commit 541798c

Browse files
author
Allen Hsu
committed
Support linting self trait bounds for repitition.
1 parent 271a2e9 commit 541798c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

clippy_lints/src/trait_bounds.rs

+12
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ impl<'tcx> LateLintPass<'tcx> for TraitBounds {
120120
check_bounds_or_where_duplication(cx, gen);
121121
}
122122

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

tests/ui/repeated_where_clause_or_trait_bound.fixed

+1-1
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

+7-1
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)