Skip to content

Commit 6466258

Browse files
committed
Auto merge of #9474 - c410-f3r:arith, r=llogiq
[arithmetic-side-effects] More non-overflowing ops * Adding or Subtracting 0 * Division and Module of anything other than 0 * Multiplying 1 or 0 changelog: [arithmetic-side-effects] More non-overflowing operations
2 parents 5564158 + 611c905 commit 6466258

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,21 @@ impl ArithmeticSideEffects {
4848
if !Self::is_literal_integer(rhs, rhs_refs) {
4949
return false;
5050
}
51-
if let hir::BinOpKind::Div | hir::BinOpKind::Mul = op.node
51+
if let hir::BinOpKind::Add | hir::BinOpKind::Sub = op.node
5252
&& let hir::ExprKind::Lit(ref lit) = rhs.kind
53-
&& let ast::LitKind::Int(1, _) = lit.node
53+
&& let ast::LitKind::Int(0, _) = lit.node
54+
{
55+
return true;
56+
}
57+
if let hir::BinOpKind::Div | hir::BinOpKind::Rem = op.node
58+
&& let hir::ExprKind::Lit(ref lit) = rhs.kind
59+
&& !matches!(lit.node, ast::LitKind::Int(0, _))
60+
{
61+
return true;
62+
}
63+
if let hir::BinOpKind::Mul = op.node
64+
&& let hir::ExprKind::Lit(ref lit) = rhs.kind
65+
&& let ast::LitKind::Int(0 | 1, _) = lit.node
5466
{
5567
return true;
5668
}

tests/ui/arithmetic_side_effects.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#![allow(clippy::assign_op_pattern, clippy::unnecessary_owned_empty_strings)]
1+
#![allow(
2+
clippy::assign_op_pattern,
3+
unconditional_panic,
4+
clippy::unnecessary_owned_empty_strings
5+
)]
26
#![feature(inline_const, saturating_int_impl)]
37
#![warn(clippy::arithmetic_side_effects)]
48

@@ -41,8 +45,12 @@ pub fn non_overflowing_ops() {
4145
let _ = const { 1 + 1 };
4246

4347
let mut _a = 1;
48+
_a += 0;
49+
_a -= 0;
50+
_a /= 99;
51+
_a %= 99;
52+
_a *= 0;
4453
_a *= 1;
45-
_a /= 1;
4654
}
4755

4856
#[rustfmt::skip]
@@ -52,6 +60,14 @@ pub fn overflowing_ops() {
5260
let mut _b = 1; _b = _b + 1;
5361

5462
let mut _c = 1; _c = 1 + _c;
63+
64+
let mut _a = 1;
65+
_a += 1;
66+
_a -= 1;
67+
_a /= 0;
68+
_a %= 0;
69+
_a *= 2;
70+
_a *= 3;
5571
}
5672

5773
fn main() {}
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,58 @@
11
error: arithmetic detected
2-
--> $DIR/arithmetic_side_effects.rs:50:21
2+
--> $DIR/arithmetic_side_effects.rs:58:21
33
|
44
LL | let mut _a = 1; _a += 1;
55
| ^^^^^^^
66
|
77
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
88

99
error: arithmetic detected
10-
--> $DIR/arithmetic_side_effects.rs:52:26
10+
--> $DIR/arithmetic_side_effects.rs:60:26
1111
|
1212
LL | let mut _b = 1; _b = _b + 1;
1313
| ^^^^^^
1414

1515
error: arithmetic detected
16-
--> $DIR/arithmetic_side_effects.rs:54:26
16+
--> $DIR/arithmetic_side_effects.rs:62:26
1717
|
1818
LL | let mut _c = 1; _c = 1 + _c;
1919
| ^^^^^^
2020

21-
error: aborting due to 3 previous errors
21+
error: arithmetic detected
22+
--> $DIR/arithmetic_side_effects.rs:65:5
23+
|
24+
LL | _a += 1;
25+
| ^^^^^^^
26+
27+
error: arithmetic detected
28+
--> $DIR/arithmetic_side_effects.rs:66:5
29+
|
30+
LL | _a -= 1;
31+
| ^^^^^^^
32+
33+
error: arithmetic detected
34+
--> $DIR/arithmetic_side_effects.rs:67:5
35+
|
36+
LL | _a /= 0;
37+
| ^^^^^^^
38+
39+
error: arithmetic detected
40+
--> $DIR/arithmetic_side_effects.rs:68:5
41+
|
42+
LL | _a %= 0;
43+
| ^^^^^^^
44+
45+
error: arithmetic detected
46+
--> $DIR/arithmetic_side_effects.rs:69:5
47+
|
48+
LL | _a *= 2;
49+
| ^^^^^^^
50+
51+
error: arithmetic detected
52+
--> $DIR/arithmetic_side_effects.rs:70:5
53+
|
54+
LL | _a *= 3;
55+
| ^^^^^^^
56+
57+
error: aborting due to 9 previous errors
2258

0 commit comments

Comments
 (0)