Skip to content

Commit e97602e

Browse files
committed
Update existing arithmetic lint and add new tests related to it.
1 parent 6d5cd6e commit e97602e

File tree

3 files changed

+112
-28
lines changed

3 files changed

+112
-28
lines changed

clippy_lints/src/arithmetic.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,33 @@ impl<'tcx> LateLintPass<'tcx> for Arithmetic {
8888

8989
let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
9090
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
91-
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
92-
self.expr_span = Some(expr.span);
93-
} else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
91+
match op.node {
92+
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
93+
hir::ExprKind::Lit(lit) => {
94+
if let rustc_ast::ast::LitKind::Int(0, _) = lit.node {
95+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
96+
self.expr_span = Some(expr.span);
97+
}
98+
},
99+
hir::ExprKind::Unary(hir::UnOp::UnNeg, expr) => {
100+
if let hir::ExprKind::Lit(lit) = &expr.kind {
101+
if let rustc_ast::ast::LitKind::Int(1, _) = lit.node {
102+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
103+
self.expr_span = Some(expr.span);
104+
}
105+
}
106+
},
107+
_ => {
108+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
109+
self.expr_span = Some(expr.span);
110+
},
111+
},
112+
_ => {
113+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
114+
self.expr_span = Some(expr.span);
115+
},
116+
}
117+
} else if r_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
94118
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
95119
self.expr_span = Some(expr.span);
96120
}

tests/ui/integer_arithmetic.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#[rustfmt::skip]
1212
fn main() {
1313
let mut i = 1i32;
14+
let mut var1 = 0i32;
15+
let mut var2 = -1i32;
1416
1 + i;
1517
i * 2;
1618
1 %
@@ -32,7 +34,15 @@ fn main() {
3234
i -= 1;
3335
i *= 2;
3436
i /= 2;
37+
i /= 0;
38+
i /= -1;
39+
i /= var1;
40+
i /= var2;
3541
i %= 2;
42+
i %= 0;
43+
i %= -1;
44+
i %= var1;
45+
i %= var2;
3646
i <<= 3;
3747
i >>= 2;
3848

tests/ui/integer_arithmetic.stderr

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,181 @@
1+
error: this operation will panic at runtime
2+
--> $DIR/integer_arithmetic.rs:37:5
3+
|
4+
LL | i /= 0;
5+
| ^^^^^^ attempt to divide `_` by zero
6+
|
7+
= note: `#[deny(unconditional_panic)]` on by default
8+
9+
error: this operation will panic at runtime
10+
--> $DIR/integer_arithmetic.rs:42:5
11+
|
12+
LL | i %= 0;
13+
| ^^^^^^ attempt to calculate the remainder of `_` with a divisor of zero
14+
115
error: integer arithmetic detected
2-
--> $DIR/integer_arithmetic.rs:14:5
16+
--> $DIR/integer_arithmetic.rs:16:5
317
|
418
LL | 1 + i;
519
| ^^^^^
620
|
721
= note: `-D clippy::integer-arithmetic` implied by `-D warnings`
822

923
error: integer arithmetic detected
10-
--> $DIR/integer_arithmetic.rs:15:5
24+
--> $DIR/integer_arithmetic.rs:17:5
1125
|
1226
LL | i * 2;
1327
| ^^^^^
1428

1529
error: integer arithmetic detected
16-
--> $DIR/integer_arithmetic.rs:16:5
30+
--> $DIR/integer_arithmetic.rs:18:5
1731
|
1832
LL | / 1 %
1933
LL | | i / 2; // no error, this is part of the expression in the preceding line
20-
| |_________^
34+
| |_____^
2135

2236
error: integer arithmetic detected
23-
--> $DIR/integer_arithmetic.rs:18:5
37+
--> $DIR/integer_arithmetic.rs:20:5
2438
|
2539
LL | i - 2 + 2 - i;
2640
| ^^^^^^^^^^^^^
2741

2842
error: integer arithmetic detected
29-
--> $DIR/integer_arithmetic.rs:19:5
43+
--> $DIR/integer_arithmetic.rs:21:5
3044
|
3145
LL | -i;
3246
| ^^
3347

3448
error: integer arithmetic detected
35-
--> $DIR/integer_arithmetic.rs:20:5
49+
--> $DIR/integer_arithmetic.rs:22:5
3650
|
3751
LL | i >> 1;
3852
| ^^^^^^
3953

4054
error: integer arithmetic detected
41-
--> $DIR/integer_arithmetic.rs:21:5
55+
--> $DIR/integer_arithmetic.rs:23:5
4256
|
4357
LL | i << 1;
4458
| ^^^^^^
4559

4660
error: integer arithmetic detected
47-
--> $DIR/integer_arithmetic.rs:31:5
61+
--> $DIR/integer_arithmetic.rs:33:5
4862
|
4963
LL | i += 1;
5064
| ^^^^^^
5165

5266
error: integer arithmetic detected
53-
--> $DIR/integer_arithmetic.rs:32:5
67+
--> $DIR/integer_arithmetic.rs:34:5
5468
|
5569
LL | i -= 1;
5670
| ^^^^^^
5771

5872
error: integer arithmetic detected
59-
--> $DIR/integer_arithmetic.rs:33:5
73+
--> $DIR/integer_arithmetic.rs:35:5
6074
|
6175
LL | i *= 2;
6276
| ^^^^^^
6377

6478
error: integer arithmetic detected
65-
--> $DIR/integer_arithmetic.rs:34:5
79+
--> $DIR/integer_arithmetic.rs:37:5
6680
|
67-
LL | i /= 2;
81+
LL | i /= 0;
6882
| ^^^^^^
6983

7084
error: integer arithmetic detected
71-
--> $DIR/integer_arithmetic.rs:35:5
85+
--> $DIR/integer_arithmetic.rs:38:11
86+
|
87+
LL | i /= -1;
88+
| ^
89+
90+
error: integer arithmetic detected
91+
--> $DIR/integer_arithmetic.rs:39:5
92+
|
93+
LL | i /= var1;
94+
| ^^^^^^^^^
95+
96+
error: integer arithmetic detected
97+
--> $DIR/integer_arithmetic.rs:40:5
98+
|
99+
LL | i /= var2;
100+
| ^^^^^^^^^
101+
102+
error: integer arithmetic detected
103+
--> $DIR/integer_arithmetic.rs:42:5
72104
|
73-
LL | i %= 2;
105+
LL | i %= 0;
74106
| ^^^^^^
75107

76108
error: integer arithmetic detected
77-
--> $DIR/integer_arithmetic.rs:36:5
109+
--> $DIR/integer_arithmetic.rs:43:11
110+
|
111+
LL | i %= -1;
112+
| ^
113+
114+
error: integer arithmetic detected
115+
--> $DIR/integer_arithmetic.rs:44:5
116+
|
117+
LL | i %= var1;
118+
| ^^^^^^^^^
119+
120+
error: integer arithmetic detected
121+
--> $DIR/integer_arithmetic.rs:45:5
122+
|
123+
LL | i %= var2;
124+
| ^^^^^^^^^
125+
126+
error: integer arithmetic detected
127+
--> $DIR/integer_arithmetic.rs:46:5
78128
|
79129
LL | i <<= 3;
80130
| ^^^^^^^
81131

82132
error: integer arithmetic detected
83-
--> $DIR/integer_arithmetic.rs:37:5
133+
--> $DIR/integer_arithmetic.rs:47:5
84134
|
85135
LL | i >>= 2;
86136
| ^^^^^^^
87137

88138
error: integer arithmetic detected
89-
--> $DIR/integer_arithmetic.rs:79:5
139+
--> $DIR/integer_arithmetic.rs:89:5
90140
|
91141
LL | 3 + &1;
92142
| ^^^^^^
93143

94144
error: integer arithmetic detected
95-
--> $DIR/integer_arithmetic.rs:80:5
145+
--> $DIR/integer_arithmetic.rs:90:5
96146
|
97147
LL | &3 + 1;
98148
| ^^^^^^
99149

100150
error: integer arithmetic detected
101-
--> $DIR/integer_arithmetic.rs:81:5
151+
--> $DIR/integer_arithmetic.rs:91:5
102152
|
103153
LL | &3 + &1;
104154
| ^^^^^^^
105155

106156
error: integer arithmetic detected
107-
--> $DIR/integer_arithmetic.rs:86:5
157+
--> $DIR/integer_arithmetic.rs:96:5
108158
|
109159
LL | a + x
110160
| ^^^^^
111161

112162
error: integer arithmetic detected
113-
--> $DIR/integer_arithmetic.rs:90:5
163+
--> $DIR/integer_arithmetic.rs:100:5
114164
|
115165
LL | x + y
116166
| ^^^^^
117167

118168
error: integer arithmetic detected
119-
--> $DIR/integer_arithmetic.rs:94:5
169+
--> $DIR/integer_arithmetic.rs:104:5
120170
|
121171
LL | x + y
122172
| ^^^^^
123173

124174
error: integer arithmetic detected
125-
--> $DIR/integer_arithmetic.rs:98:5
175+
--> $DIR/integer_arithmetic.rs:108:5
126176
|
127177
LL | (&x + &y)
128178
| ^^^^^^^^^
129179

130-
error: aborting due to 21 previous errors
180+
error: aborting due to 29 previous errors
131181

0 commit comments

Comments
 (0)