Skip to content

Commit 04be286

Browse files
committed
Disallow bit-shifting in integer_arithmetic lint
With this change, the lint checks all operations that are defined as being capable of overflow in the Rust Reference.
1 parent e29d550 commit 04be286

File tree

4 files changed

+167
-21
lines changed

4 files changed

+167
-21
lines changed

clippy_lints/src/arithmetic.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ use rustc_session::{declare_tool_lint, impl_lint_pass};
66
use rustc_span::source_map::Span;
77

88
declare_clippy_lint! {
9-
/// **What it does:** Checks for plain integer arithmetic.
9+
/// **What it does:** Checks for integer arithmetic operations which could overflow or panic.
1010
///
11-
/// **Why is this bad?** This is only checked against overflow in debug builds.
12-
/// In some applications one wants explicitly checked, wrapping or saturating
13-
/// arithmetic.
11+
/// Specifically, checks for any operators (`+`, `-`, `*`, `<<`, etc) which are capable
12+
/// of overflowing according to the [Rust
13+
/// Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
14+
/// or which can panic (`/`, `%`). No bounds analysis or sophisticated reasoning is
15+
/// attempted.
16+
///
17+
/// **Why is this bad?** Integer overflow will trigger a panic in debug builds or will wrap in
18+
/// release mode. Division by zero will cause a panic in either mode. In some applications one
19+
/// wants explicitly checked, wrapping or saturating arithmetic.
1420
///
1521
/// **Known problems:** None.
1622
///
@@ -21,7 +27,7 @@ declare_clippy_lint! {
2127
/// ```
2228
pub INTEGER_ARITHMETIC,
2329
restriction,
24-
"any integer arithmetic statement"
30+
"any integer arithmetic expression which could overflow or panic"
2531
}
2632

2733
declare_clippy_lint! {
@@ -71,8 +77,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
7177
| hir::BinOpKind::BitAnd
7278
| hir::BinOpKind::BitOr
7379
| hir::BinOpKind::BitXor
74-
| hir::BinOpKind::Shl
75-
| hir::BinOpKind::Shr
7680
| hir::BinOpKind::Eq
7781
| hir::BinOpKind::Lt
7882
| hir::BinOpKind::Le

src/lintlist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
846846
Lint {
847847
name: "integer_arithmetic",
848848
group: "restriction",
849-
desc: "any integer arithmetic statement",
849+
desc: "any integer arithmetic expression which could overflow or panic",
850850
deprecation: None,
851851
module: "arithmetic",
852852
},

tests/ui/integer_arithmetic.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fn main() {
1717
i / 2; // no error, this is part of the expression in the preceding line
1818
i - 2 + 2 - i;
1919
-i;
20+
i >> 1;
21+
i << 1;
2022

2123
// no error, overflows are checked by `overflowing_literals`
2224
-1;
@@ -25,18 +27,16 @@ fn main() {
2527
i & 1; // no wrapping
2628
i | 1;
2729
i ^ 1;
28-
i >> 1;
29-
i << 1;
3030

3131
i += 1;
3232
i -= 1;
3333
i *= 2;
3434
i /= 2;
3535
i %= 2;
36-
37-
// no errors
3836
i <<= 3;
3937
i >>= 2;
38+
39+
// no errors
4040
i |= 1;
4141
i &= 1;
4242
i ^= i;
@@ -72,8 +72,6 @@ fn main() {
7272
1 + 1
7373
};
7474
}
75-
76-
7775
}
7876

7977
// warn on references as well! (#5328)
@@ -83,6 +81,42 @@ pub fn int_arith_ref() {
8381
&3 + &1;
8482
}
8583

84+
pub fn unsigned() {
85+
let mut i = 1000u64;
86+
87+
// should error
88+
i + 2;
89+
i - 2;
90+
i * 2;
91+
i << 2;
92+
i >> i;
93+
i / 2;
94+
i % 5;
95+
96+
// should error
97+
i += 2;
98+
i -= 2;
99+
i *= i;
100+
i <<= 2;
101+
i >>= 2;
102+
i /= i;
103+
i %= 6;
104+
105+
// should not error
106+
i | 3;
107+
i & 2;
108+
i ^ 4;
109+
110+
// should not error
111+
i |= 1;
112+
i &= 1;
113+
i ^= i;
114+
115+
// should not error
116+
!i;
117+
!(!i);
118+
}
119+
86120
pub fn foo(x: &i32) -> i32 {
87121
let a = 5;
88122
a + x

tests/ui/integer_arithmetic.stderr

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ error: integer arithmetic detected
3131
LL | -i;
3232
| ^^
3333

34+
error: integer arithmetic detected
35+
--> $DIR/integer_arithmetic.rs:20:5
36+
|
37+
LL | i >> 1;
38+
| ^^^^^^
39+
40+
error: integer arithmetic detected
41+
--> $DIR/integer_arithmetic.rs:21:5
42+
|
43+
LL | i << 1;
44+
| ^^^^^^
45+
3446
error: integer arithmetic detected
3547
--> $DIR/integer_arithmetic.rs:31:5
3648
|
@@ -62,46 +74,142 @@ LL | i %= 2;
6274
| ^^^^^^
6375

6476
error: integer arithmetic detected
65-
--> $DIR/integer_arithmetic.rs:81:5
77+
--> $DIR/integer_arithmetic.rs:36:5
78+
|
79+
LL | i <<= 3;
80+
| ^^^^^^^
81+
82+
error: integer arithmetic detected
83+
--> $DIR/integer_arithmetic.rs:37:5
84+
|
85+
LL | i >>= 2;
86+
| ^^^^^^^
87+
88+
error: integer arithmetic detected
89+
--> $DIR/integer_arithmetic.rs:79:5
6690
|
6791
LL | 3 + &1;
6892
| ^^^^^^
6993

7094
error: integer arithmetic detected
71-
--> $DIR/integer_arithmetic.rs:82:5
95+
--> $DIR/integer_arithmetic.rs:80:5
7296
|
7397
LL | &3 + 1;
7498
| ^^^^^^
7599

76100
error: integer arithmetic detected
77-
--> $DIR/integer_arithmetic.rs:83:5
101+
--> $DIR/integer_arithmetic.rs:81:5
78102
|
79103
LL | &3 + &1;
80104
| ^^^^^^^
81105

82106
error: integer arithmetic detected
83107
--> $DIR/integer_arithmetic.rs:88:5
84108
|
85-
LL | a + x
109+
LL | i + 2;
110+
| ^^^^^
111+
112+
error: integer arithmetic detected
113+
--> $DIR/integer_arithmetic.rs:89:5
114+
|
115+
LL | i - 2;
116+
| ^^^^^
117+
118+
error: integer arithmetic detected
119+
--> $DIR/integer_arithmetic.rs:90:5
120+
|
121+
LL | i * 2;
86122
| ^^^^^
87123

124+
error: integer arithmetic detected
125+
--> $DIR/integer_arithmetic.rs:91:5
126+
|
127+
LL | i << 2;
128+
| ^^^^^^
129+
88130
error: integer arithmetic detected
89131
--> $DIR/integer_arithmetic.rs:92:5
90132
|
133+
LL | i >> i;
134+
| ^^^^^^
135+
136+
error: integer arithmetic detected
137+
--> $DIR/integer_arithmetic.rs:93:5
138+
|
139+
LL | i / 2;
140+
| ^^^^^
141+
142+
error: integer arithmetic detected
143+
--> $DIR/integer_arithmetic.rs:94:5
144+
|
145+
LL | i % 5;
146+
| ^^^^^
147+
148+
error: integer arithmetic detected
149+
--> $DIR/integer_arithmetic.rs:97:5
150+
|
151+
LL | i += 2;
152+
| ^^^^^^
153+
154+
error: integer arithmetic detected
155+
--> $DIR/integer_arithmetic.rs:98:5
156+
|
157+
LL | i -= 2;
158+
| ^^^^^^
159+
160+
error: integer arithmetic detected
161+
--> $DIR/integer_arithmetic.rs:99:5
162+
|
163+
LL | i *= i;
164+
| ^^^^^^
165+
166+
error: integer arithmetic detected
167+
--> $DIR/integer_arithmetic.rs:100:5
168+
|
169+
LL | i <<= 2;
170+
| ^^^^^^^
171+
172+
error: integer arithmetic detected
173+
--> $DIR/integer_arithmetic.rs:101:5
174+
|
175+
LL | i >>= 2;
176+
| ^^^^^^^
177+
178+
error: integer arithmetic detected
179+
--> $DIR/integer_arithmetic.rs:102:5
180+
|
181+
LL | i /= i;
182+
| ^^^^^^
183+
184+
error: integer arithmetic detected
185+
--> $DIR/integer_arithmetic.rs:103:5
186+
|
187+
LL | i %= 6;
188+
| ^^^^^^
189+
190+
error: integer arithmetic detected
191+
--> $DIR/integer_arithmetic.rs:122:5
192+
|
193+
LL | a + x
194+
| ^^^^^
195+
196+
error: integer arithmetic detected
197+
--> $DIR/integer_arithmetic.rs:126:5
198+
|
91199
LL | x + y
92200
| ^^^^^
93201

94202
error: integer arithmetic detected
95-
--> $DIR/integer_arithmetic.rs:96:5
203+
--> $DIR/integer_arithmetic.rs:130:5
96204
|
97205
LL | x + y
98206
| ^^^^^
99207

100208
error: integer arithmetic detected
101-
--> $DIR/integer_arithmetic.rs:100:5
209+
--> $DIR/integer_arithmetic.rs:134:5
102210
|
103211
LL | (&x + &y)
104212
| ^^^^^^^^^
105213

106-
error: aborting due to 17 previous errors
214+
error: aborting due to 35 previous errors
107215

0 commit comments

Comments
 (0)