Skip to content

Commit b7cb075

Browse files
committed
Improved suggestion on misrefactored_assign_op lint. Fixes #1239
1 parent 39d1d60 commit b7cb075

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

clippy_lints/src/assign_ops.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
8787
});
8888
if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
8989
if op.node == binop.node {
90-
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
90+
let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
9191
span_lint_and_then(
9292
cx,
9393
MISREFACTORED_ASSIGN_OP,
9494
expr.span,
9595
"variable appears on both sides of an assignment operation",
9696
|db| if let (Some(snip_a), Some(snip_r)) =
97-
(snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
97+
(snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
9898
{
99+
let a = &sugg::Sugg::hir(cx, assignee, "..");
100+
let r = &sugg::Sugg::hir(cx, rhs, "..");
99101
db.span_suggestion(
100102
expr.span,
101-
"replace it with",
102-
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
103+
&format!("Did you mean {} = {} {} {} or {} = {}? Consider replacing it with",
104+
snip_a, snip_a, op.node.as_str(), snip_r,
105+
snip_a, sugg::make_binop(higher::binop(op.node), a, r)),
106+
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r)
103107
);
104108
},
105109
);

tests/ui/assign_ops2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn main() {
1313
a /= a / 2;
1414
a %= a % 5;
1515
a &= a & 1;
16+
a *= a * a;
1617
a -= 1 - a;
1718
a /= 5 / a;
1819
a %= 42 % a;

tests/ui/assign_ops2.stderr

+51-9
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,93 @@ error: variable appears on both sides of an assignment operation
22
--> $DIR/assign_ops2.rs:8:5
33
|
44
8 | a += a + 1;
5-
| ^^^^^^^^^^ help: replace it with: `a += 1`
5+
| ^^^^^^^^^^
66
|
77
= note: `-D misrefactored-assign-op` implied by `-D warnings`
8+
help: Did you mean a = a + 1 or a = a + a + 1? Consider replacing it with
9+
|
10+
8 | a += 1;
11+
| ^^^^^^
812

913
error: variable appears on both sides of an assignment operation
1014
--> $DIR/assign_ops2.rs:9:5
1115
|
1216
9 | a += 1 + a;
13-
| ^^^^^^^^^^ help: replace it with: `a += 1`
17+
| ^^^^^^^^^^
18+
help: Did you mean a = a + 1 or a = a + 1 + a? Consider replacing it with
19+
|
20+
9 | a += 1;
21+
| ^^^^^^
1422

1523
error: variable appears on both sides of an assignment operation
1624
--> $DIR/assign_ops2.rs:10:5
1725
|
1826
10 | a -= a - 1;
19-
| ^^^^^^^^^^ help: replace it with: `a -= 1`
27+
| ^^^^^^^^^^
28+
help: Did you mean a = a - 1 or a = a - (a - 1)? Consider replacing it with
29+
|
30+
10 | a -= 1;
31+
| ^^^^^^
2032

2133
error: variable appears on both sides of an assignment operation
2234
--> $DIR/assign_ops2.rs:11:5
2335
|
2436
11 | a *= a * 99;
25-
| ^^^^^^^^^^^ help: replace it with: `a *= 99`
37+
| ^^^^^^^^^^^
38+
help: Did you mean a = a * 99 or a = a * a * 99? Consider replacing it with
39+
|
40+
11 | a *= 99;
41+
| ^^^^^^^
2642

2743
error: variable appears on both sides of an assignment operation
2844
--> $DIR/assign_ops2.rs:12:5
2945
|
3046
12 | a *= 42 * a;
31-
| ^^^^^^^^^^^ help: replace it with: `a *= 42`
47+
| ^^^^^^^^^^^
48+
help: Did you mean a = a * 42 or a = a * 42 * a? Consider replacing it with
49+
|
50+
12 | a *= 42;
51+
| ^^^^^^^
3252

3353
error: variable appears on both sides of an assignment operation
3454
--> $DIR/assign_ops2.rs:13:5
3555
|
3656
13 | a /= a / 2;
37-
| ^^^^^^^^^^ help: replace it with: `a /= 2`
57+
| ^^^^^^^^^^
58+
help: Did you mean a = a / 2 or a = a / (a / 2)? Consider replacing it with
59+
|
60+
13 | a /= 2;
61+
| ^^^^^^
3862

3963
error: variable appears on both sides of an assignment operation
4064
--> $DIR/assign_ops2.rs:14:5
4165
|
4266
14 | a %= a % 5;
43-
| ^^^^^^^^^^ help: replace it with: `a %= 5`
67+
| ^^^^^^^^^^
68+
help: Did you mean a = a % 5 or a = a % (a % 5)? Consider replacing it with
69+
|
70+
14 | a %= 5;
71+
| ^^^^^^
4472

4573
error: variable appears on both sides of an assignment operation
4674
--> $DIR/assign_ops2.rs:15:5
4775
|
4876
15 | a &= a & 1;
49-
| ^^^^^^^^^^ help: replace it with: `a &= 1`
77+
| ^^^^^^^^^^
78+
help: Did you mean a = a & 1 or a = a & a & 1? Consider replacing it with
79+
|
80+
15 | a &= 1;
81+
| ^^^^^^
82+
83+
error: variable appears on both sides of an assignment operation
84+
--> $DIR/assign_ops2.rs:16:5
85+
|
86+
16 | a *= a * a;
87+
| ^^^^^^^^^^
88+
help: Did you mean a = a * a or a = a * a * a? Consider replacing it with
89+
|
90+
16 | a *= a;
91+
| ^^^^^^
5092

51-
error: aborting due to 8 previous errors
93+
error: aborting due to 9 previous errors
5294

0 commit comments

Comments
 (0)