Skip to content

Commit

Permalink
Detect assignment ops in integer_arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Sep 27, 2019
1 parent 68ff8b1 commit 4f9d6ee
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions tests/ui/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#[rustfmt::skip]
fn main() {
let i = 1i32;
let mut i = 1i32;
1 + i;
i * 2;
1 %
Expand All @@ -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;

Expand All @@ -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,
Expand Down
68 changes: 61 additions & 7 deletions tests/ui/arithmetic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4f9d6ee

Please sign in to comment.