Skip to content

Commit f7d7456

Browse files
committed
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
2 parents 68ff8b1 + 4f9d6ee commit f7d7456

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

clippy_lints/src/arithmetic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
6464
}
6565
}
6666
match &expr.node {
67-
hir::ExprKind::Binary(op, l, r) => {
67+
hir::ExprKind::Binary(op, l, r) | hir::ExprKind::AssignOp(op, l, r) => {
6868
match op.node {
6969
hir::BinOpKind::And
7070
| hir::BinOpKind::Or

tests/ui/arithmetic.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#[rustfmt::skip]
1111
fn main() {
12-
let i = 1i32;
12+
let mut i = 1i32;
1313
1 + i;
1414
i * 2;
1515
1 %
@@ -27,7 +27,20 @@ fn main() {
2727
i >> 1;
2828
i << 1;
2929

30-
let f = 1.0f32;
30+
i += 1;
31+
i -= 1;
32+
i *= 2;
33+
i /= 2;
34+
i %= 2;
35+
36+
// no errors
37+
i <<= 3;
38+
i >>= 2;
39+
i |= 1;
40+
i &= 1;
41+
i ^= i;
42+
43+
let mut f = 1.0f32;
3144

3245
f * 2.0;
3346

@@ -37,6 +50,11 @@ fn main() {
3750
f - 2.0 * 4.2;
3851
-f;
3952

53+
f += 1.0;
54+
f -= 1.0;
55+
f *= 2.0;
56+
f /= 2.0;
57+
4058
// No errors for the following items because they are constant expressions
4159
enum Foo {
4260
Bar = -2,

tests/ui/arithmetic.stderr

+61-7
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,97 @@ error: integer arithmetic detected
3131
LL | -i;
3232
| ^^
3333

34-
error: floating-point arithmetic detected
34+
error: integer arithmetic detected
35+
--> $DIR/arithmetic.rs:30:5
36+
|
37+
LL | i += 1;
38+
| ^^^^^^
39+
40+
error: integer arithmetic detected
41+
--> $DIR/arithmetic.rs:31:5
42+
|
43+
LL | i -= 1;
44+
| ^^^^^^
45+
46+
error: integer arithmetic detected
3547
--> $DIR/arithmetic.rs:32:5
3648
|
49+
LL | i *= 2;
50+
| ^^^^^^
51+
52+
error: integer arithmetic detected
53+
--> $DIR/arithmetic.rs:33:5
54+
|
55+
LL | i /= 2;
56+
| ^^^^^^
57+
58+
error: integer arithmetic detected
59+
--> $DIR/arithmetic.rs:34:5
60+
|
61+
LL | i %= 2;
62+
| ^^^^^^
63+
64+
error: floating-point arithmetic detected
65+
--> $DIR/arithmetic.rs:45:5
66+
|
3767
LL | f * 2.0;
3868
| ^^^^^^^
3969
|
4070
= note: `-D clippy::float-arithmetic` implied by `-D warnings`
4171

4272
error: floating-point arithmetic detected
43-
--> $DIR/arithmetic.rs:34:5
73+
--> $DIR/arithmetic.rs:47:5
4474
|
4575
LL | 1.0 + f;
4676
| ^^^^^^^
4777

4878
error: floating-point arithmetic detected
49-
--> $DIR/arithmetic.rs:35:5
79+
--> $DIR/arithmetic.rs:48:5
5080
|
5181
LL | f * 2.0;
5282
| ^^^^^^^
5383

5484
error: floating-point arithmetic detected
55-
--> $DIR/arithmetic.rs:36:5
85+
--> $DIR/arithmetic.rs:49:5
5686
|
5787
LL | f / 2.0;
5888
| ^^^^^^^
5989

6090
error: floating-point arithmetic detected
61-
--> $DIR/arithmetic.rs:37:5
91+
--> $DIR/arithmetic.rs:50:5
6292
|
6393
LL | f - 2.0 * 4.2;
6494
| ^^^^^^^^^^^^^
6595

6696
error: floating-point arithmetic detected
67-
--> $DIR/arithmetic.rs:38:5
97+
--> $DIR/arithmetic.rs:51:5
6898
|
6999
LL | -f;
70100
| ^^
71101

72-
error: aborting due to 11 previous errors
102+
error: floating-point arithmetic detected
103+
--> $DIR/arithmetic.rs:53:5
104+
|
105+
LL | f += 1.0;
106+
| ^^^^^^^^
107+
108+
error: floating-point arithmetic detected
109+
--> $DIR/arithmetic.rs:54:5
110+
|
111+
LL | f -= 1.0;
112+
| ^^^^^^^^
113+
114+
error: floating-point arithmetic detected
115+
--> $DIR/arithmetic.rs:55:5
116+
|
117+
LL | f *= 2.0;
118+
| ^^^^^^^^
119+
120+
error: floating-point arithmetic detected
121+
--> $DIR/arithmetic.rs:56:5
122+
|
123+
LL | f /= 2.0;
124+
| ^^^^^^^^
125+
126+
error: aborting due to 20 previous errors
73127

0 commit comments

Comments
 (0)