Skip to content

Commit 67657da

Browse files
authored
Handle repetition of associated constant constraint as well (rust-lang#13723)
changelog: [`trait_duplication_in_bounds`]: trigger on duplicate const associated constraint as well ~~The first commit is part of rust-lang#13722 which must be merged first.~~
2 parents 9b0597d + 2bf03f1 commit 67657da

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

clippy_utils/src/hir_utils.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl HirEqInterExpr<'_, '_, '_> {
545545
fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
546546
if left.parenthesized == right.parenthesized {
547547
over(left.args, right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
548-
&& over(left.constraints, right.constraints, |l, r| self.eq_assoc_type_binding(l, r))
548+
&& over(left.constraints, right.constraints, |l, r| self.eq_assoc_eq_constraint(l, r))
549549
} else {
550550
false
551551
}
@@ -602,8 +602,13 @@ impl HirEqInterExpr<'_, '_, '_> {
602602
}
603603
}
604604

605-
fn eq_assoc_type_binding(&mut self, left: &AssocItemConstraint<'_>, right: &AssocItemConstraint<'_>) -> bool {
606-
left.ident.name == right.ident.name && both_some_and(left.ty(), right.ty(), |l, r| self.eq_ty(l, r))
605+
/// Checks whether two constraints designate the same equality constraint (same name, and same
606+
/// type or const).
607+
fn eq_assoc_eq_constraint(&mut self, left: &AssocItemConstraint<'_>, right: &AssocItemConstraint<'_>) -> bool {
608+
// TODO: this could be extended to check for identical associated item bound constraints
609+
left.ident.name == right.ident.name
610+
&& (both_some_and(left.ty(), right.ty(), |l, r| self.eq_ty(l, r))
611+
|| both_some_and(left.ct(), right.ct(), |l, r| self.eq_const_arg(l, r)))
607612
}
608613

609614
fn check_ctxt(&mut self, left: SyntaxContext, right: SyntaxContext) -> bool {

tests/ui/trait_duplication_in_bounds.fixed

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ trait AssocConstTrait {
191191
}
192192
fn assoc_const_args<T>()
193193
where
194-
T: AssocConstTrait<ASSOC = 0> + AssocConstTrait<ASSOC = 0>,
194+
T: AssocConstTrait<ASSOC = 0>,
195+
//~^ trait_duplication_in_bounds
195196
{
196197
}

tests/ui/trait_duplication_in_bounds.rs

+1
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ trait AssocConstTrait {
192192
fn assoc_const_args<T>()
193193
where
194194
T: AssocConstTrait<ASSOC = 0> + AssocConstTrait<ASSOC = 0>,
195+
//~^ trait_duplication_in_bounds
195196
{
196197
}

tests/ui/trait_duplication_in_bounds.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,11 @@ error: these where clauses contain repeated elements
7070
LL | T: IntoIterator<Item = U::Owned> + IntoIterator<Item = U::Owned>,
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `IntoIterator<Item = U::Owned>`
7272

73-
error: aborting due to 11 previous errors
73+
error: these where clauses contain repeated elements
74+
--> tests/ui/trait_duplication_in_bounds.rs:194:8
75+
|
76+
LL | T: AssocConstTrait<ASSOC = 0> + AssocConstTrait<ASSOC = 0>,
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `AssocConstTrait<ASSOC = 0>`
78+
79+
error: aborting due to 12 previous errors
7480

0 commit comments

Comments
 (0)