Auto merge of #4585 - michaelsproul:arith-assign-op, r=llogiq

Detect mutating arithmetic in integer_arithmetic restriction lint

changelog: detect mutating arithmetic (like +=) in `integer_arithmetic` restriction lint
This commit is contained in:
bors 2019-09-27 04:46:06 +00:00
commit f7d7456303
3 changed files with 82 additions and 10 deletions

View File

@ -64,7 +64,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
}
}
match &expr.node {
hir::ExprKind::Binary(op, l, r) => {
hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
match op.node {
hir::BinOpKind::And
| hir::BinOpKind::Or

View File

@ -9,7 +9,7 @@
#[rustfmt::skip]
fn main() {
let i = 1i32;
let mut i = 1i32;
1 + i;
i * 2;
1 %
@ -27,7 +27,20 @@ fn main() {
i >> 1;
i << 1;
let f = 1.0f32;
i += 1;
i -= 1;
i *= 2;
i /= 2;
i %= 2;
// no errors
i <<= 3;
i >>= 2;
i |= 1;
i &= 1;
i ^= i;
let mut f = 1.0f32;
f * 2.0;
@ -37,6 +50,11 @@ fn main() {
f - 2.0 * 4.2;
-f;
f += 1.0;
f -= 1.0;
f *= 2.0;
f /= 2.0;
// No errors for the following items because they are constant expressions
enum Foo {
Bar = -2,

View File

@ -31,43 +31,97 @@ error: integer arithmetic detected
LL | -i;
| ^^
error: floating-point arithmetic detected
error: integer arithmetic detected
--> $DIR/arithmetic.rs:30:5
|
LL | i += 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:31:5
|
LL | i -= 1;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:32:5
|
LL | i *= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:33:5
|
LL | i /= 2;
| ^^^^^^
error: integer arithmetic detected
--> $DIR/arithmetic.rs:34:5
|
LL | i %= 2;
| ^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:45:5
|
LL | f * 2.0;
| ^^^^^^^
|
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:34:5
--> $DIR/arithmetic.rs:47:5
|
LL | 1.0 + f;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:35:5
--> $DIR/arithmetic.rs:48:5
|
LL | f * 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:36:5
--> $DIR/arithmetic.rs:49:5
|
LL | f / 2.0;
| ^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:37:5
--> $DIR/arithmetic.rs:50:5
|
LL | f - 2.0 * 4.2;
| ^^^^^^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:38:5
--> $DIR/arithmetic.rs:51:5
|
LL | -f;
| ^^
error: aborting due to 11 previous errors
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:53:5
|
LL | f += 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:54:5
|
LL | f -= 1.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:55:5
|
LL | f *= 2.0;
| ^^^^^^^^
error: floating-point arithmetic detected
--> $DIR/arithmetic.rs:56:5
|
LL | f /= 2.0;
| ^^^^^^^^
error: aborting due to 20 previous errors