Skip to content

Commit

Permalink
Auto merge of rust-lang#12413 - high-cloud:fix_assign_ops2, r=flip1995
Browse files Browse the repository at this point in the history
[`misrefactored_assign_op`]: Fix duplicate diagnostics

Relate to rust-lang#12379

The following diagnostics appear twice
```
  --> tests/ui/assign_ops2.rs:26:5
   |
LL |     a *= a * a;
   |     ^^^^^^^^^^
   |
help: did you mean `a = a * a` or `a = a * a * a`? Consider replacing it with
```

because `a` (lhs) appears in both left operand and right operand in the right hand side.
This PR fixes the issue so that if a diagnostic is created for an operand, the check of the other operand will be skipped. It's fine because the result is always the same in the affected operators.

changelog: [`misrefactored_assign_op`]: Fix duplicate diagnostics
  • Loading branch information
bors committed Mar 5, 2024
2 parents 21efd39 + 3c5008e commit 2d9d404
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
5 changes: 2 additions & 3 deletions clippy_lints/src/operators/misrefactored_assign_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ pub(super) fn check<'tcx>(
// lhs op= l op r
if eq_expr_value(cx, lhs, l) {
lint_misrefactored_assign_op(cx, expr, op, rhs, lhs, r);
}
// lhs op= l commutative_op r
if is_commutative(op) && eq_expr_value(cx, lhs, r) {
} else if is_commutative(op) && eq_expr_value(cx, lhs, r) {
// lhs op= l commutative_op r
lint_misrefactored_assign_op(cx, expr, op, rhs, lhs, l);
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/assign_ops2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@no-rustfix: overlapping suggestions
//@compile-flags: -Zdeduplicate-diagnostics=yes

#![allow(clippy::uninlined_format_args)]

#[allow(unused_assignments)]
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/assign_ops2.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:10:5
--> tests/ui/assign_ops2.rs:8:5
|
LL | a += a + 1;
| ^^^^^^^^^^
Expand All @@ -16,7 +16,7 @@ LL | a = a + a + 1;
| ~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:13:5
--> tests/ui/assign_ops2.rs:11:5
|
LL | a += 1 + a;
| ^^^^^^^^^^
Expand All @@ -31,7 +31,7 @@ LL | a = a + 1 + a;
| ~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:15:5
--> tests/ui/assign_ops2.rs:13:5
|
LL | a -= a - 1;
| ^^^^^^^^^^
Expand All @@ -46,7 +46,7 @@ LL | a = a - (a - 1);
| ~~~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:17:5
--> tests/ui/assign_ops2.rs:15:5
|
LL | a *= a * 99;
| ^^^^^^^^^^^
Expand All @@ -61,7 +61,7 @@ LL | a = a * a * 99;
| ~~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:19:5
--> tests/ui/assign_ops2.rs:17:5
|
LL | a *= 42 * a;
| ^^^^^^^^^^^
Expand All @@ -76,7 +76,7 @@ LL | a = a * 42 * a;
| ~~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:21:5
--> tests/ui/assign_ops2.rs:19:5
|
LL | a /= a / 2;
| ^^^^^^^^^^
Expand All @@ -91,7 +91,7 @@ LL | a = a / (a / 2);
| ~~~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:23:5
--> tests/ui/assign_ops2.rs:21:5
|
LL | a %= a % 5;
| ^^^^^^^^^^
Expand All @@ -106,7 +106,7 @@ LL | a = a % (a % 5);
| ~~~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:25:5
--> tests/ui/assign_ops2.rs:23:5
|
LL | a &= a & 1;
| ^^^^^^^^^^
Expand All @@ -121,7 +121,7 @@ LL | a = a & a & 1;
| ~~~~~~~~~~~~~

error: variable appears on both sides of an assignment operation
--> tests/ui/assign_ops2.rs:27:5
--> tests/ui/assign_ops2.rs:25:5
|
LL | a *= a * a;
| ^^^^^^^^^^
Expand All @@ -136,7 +136,7 @@ LL | a = a * a * a;
| ~~~~~~~~~~~~~

error: manual implementation of an assign operation
--> tests/ui/assign_ops2.rs:65:5
--> tests/ui/assign_ops2.rs:63:5
|
LL | buf = buf + cows.clone();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `buf += cows.clone()`
Expand Down

0 comments on commit 2d9d404

Please sign in to comment.