mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
Improved suggestion on misrefactored_assign_op lint. Fixes #1239
This commit is contained in:
parent
39d1d6081f
commit
b7cb0752ff
3 changed files with 60 additions and 13 deletions
|
@ -87,19 +87,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
|
|||
});
|
||||
if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
|
||||
if op.node == binop.node {
|
||||
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
|
||||
let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
MISREFACTORED_ASSIGN_OP,
|
||||
expr.span,
|
||||
"variable appears on both sides of an assignment operation",
|
||||
|db| if let (Some(snip_a), Some(snip_r)) =
|
||||
(snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
|
||||
(snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
|
||||
{
|
||||
let a = &sugg::Sugg::hir(cx, assignee, "..");
|
||||
let r = &sugg::Sugg::hir(cx, rhs, "..");
|
||||
db.span_suggestion(
|
||||
expr.span,
|
||||
"replace it with",
|
||||
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
|
||||
&format!("Did you mean {} = {} {} {} or {} = {}? Consider replacing it with",
|
||||
snip_a, snip_a, op.node.as_str(), snip_r,
|
||||
snip_a, sugg::make_binop(higher::binop(op.node), a, r)),
|
||||
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r)
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
|
@ -13,6 +13,7 @@ fn main() {
|
|||
a /= a / 2;
|
||||
a %= a % 5;
|
||||
a &= a & 1;
|
||||
a *= a * a;
|
||||
a -= 1 - a;
|
||||
a /= 5 / a;
|
||||
a %= 42 % a;
|
||||
|
|
|
@ -2,51 +2,93 @@ error: variable appears on both sides of an assignment operation
|
|||
--> $DIR/assign_ops2.rs:8:5
|
||||
|
|
||||
8 | a += a + 1;
|
||||
| ^^^^^^^^^^ help: replace it with: `a += 1`
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
= note: `-D misrefactored-assign-op` implied by `-D warnings`
|
||||
help: Did you mean a = a + 1 or a = a + a + 1? Consider replacing it with
|
||||
|
|
||||
8 | a += 1;
|
||||
| ^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:9:5
|
||||
|
|
||||
9 | a += 1 + a;
|
||||
| ^^^^^^^^^^ help: replace it with: `a += 1`
|
||||
| ^^^^^^^^^^
|
||||
help: Did you mean a = a + 1 or a = a + 1 + a? Consider replacing it with
|
||||
|
|
||||
9 | a += 1;
|
||||
| ^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:10:5
|
||||
|
|
||||
10 | a -= a - 1;
|
||||
| ^^^^^^^^^^ help: replace it with: `a -= 1`
|
||||
| ^^^^^^^^^^
|
||||
help: Did you mean a = a - 1 or a = a - (a - 1)? Consider replacing it with
|
||||
|
|
||||
10 | a -= 1;
|
||||
| ^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:11:5
|
||||
|
|
||||
11 | a *= a * 99;
|
||||
| ^^^^^^^^^^^ help: replace it with: `a *= 99`
|
||||
| ^^^^^^^^^^^
|
||||
help: Did you mean a = a * 99 or a = a * a * 99? Consider replacing it with
|
||||
|
|
||||
11 | a *= 99;
|
||||
| ^^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:12:5
|
||||
|
|
||||
12 | a *= 42 * a;
|
||||
| ^^^^^^^^^^^ help: replace it with: `a *= 42`
|
||||
| ^^^^^^^^^^^
|
||||
help: Did you mean a = a * 42 or a = a * 42 * a? Consider replacing it with
|
||||
|
|
||||
12 | a *= 42;
|
||||
| ^^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:13:5
|
||||
|
|
||||
13 | a /= a / 2;
|
||||
| ^^^^^^^^^^ help: replace it with: `a /= 2`
|
||||
| ^^^^^^^^^^
|
||||
help: Did you mean a = a / 2 or a = a / (a / 2)? Consider replacing it with
|
||||
|
|
||||
13 | a /= 2;
|
||||
| ^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:14:5
|
||||
|
|
||||
14 | a %= a % 5;
|
||||
| ^^^^^^^^^^ help: replace it with: `a %= 5`
|
||||
| ^^^^^^^^^^
|
||||
help: Did you mean a = a % 5 or a = a % (a % 5)? Consider replacing it with
|
||||
|
|
||||
14 | a %= 5;
|
||||
| ^^^^^^
|
||||
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:15:5
|
||||
|
|
||||
15 | a &= a & 1;
|
||||
| ^^^^^^^^^^ help: replace it with: `a &= 1`
|
||||
| ^^^^^^^^^^
|
||||
help: Did you mean a = a & 1 or a = a & a & 1? Consider replacing it with
|
||||
|
|
||||
15 | a &= 1;
|
||||
| ^^^^^^
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
error: variable appears on both sides of an assignment operation
|
||||
--> $DIR/assign_ops2.rs:16:5
|
||||
|
|
||||
16 | a *= a * a;
|
||||
| ^^^^^^^^^^
|
||||
help: Did you mean a = a * a or a = a * a * a? Consider replacing it with
|
||||
|
|
||||
16 | a *= a;
|
||||
| ^^^^^^
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue