Skip to content

Commit 2538e63

Browse files
authored
Rollup merge of rust-lang#5430 - michaelsproul:integer-arithmetic, r=flip1995
Disallow bit-shifting in integer_arithmetic Make the `integer_arithmetic` lint detect all the operations that are defined as being capable of overflow in the [Rust Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow), by also linting for bit-shifting operations (`<<`, `>>`). changelog: Disallow bit-shifting in `integer_arithmetic`
2 parents ceea3c6 + 23df4a0 commit 2538e63

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

clippy_lints/src/arithmetic.rs

+11-7
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
853853
Lint {
854854
name: "integer_arithmetic",
855855
group: "restriction",
856-
desc: "any integer arithmetic statement",
856+
desc: "any integer arithmetic expression which could overflow or panic",
857857
deprecation: None,
858858
module: "arithmetic",
859859
},

tests/ui/integer_arithmetic.rs

+4-6
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)

tests/ui/integer_arithmetic.stderr

+32-8
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,58 @@ 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
83-
--> $DIR/integer_arithmetic.rs:88:5
107+
--> $DIR/integer_arithmetic.rs:86:5
84108
|
85109
LL | a + x
86110
| ^^^^^
87111

88112
error: integer arithmetic detected
89-
--> $DIR/integer_arithmetic.rs:92:5
113+
--> $DIR/integer_arithmetic.rs:90:5
90114
|
91115
LL | x + y
92116
| ^^^^^
93117

94118
error: integer arithmetic detected
95-
--> $DIR/integer_arithmetic.rs:96:5
119+
--> $DIR/integer_arithmetic.rs:94:5
96120
|
97121
LL | x + y
98122
| ^^^^^
99123

100124
error: integer arithmetic detected
101-
--> $DIR/integer_arithmetic.rs:100:5
125+
--> $DIR/integer_arithmetic.rs:98:5
102126
|
103127
LL | (&x + &y)
104128
| ^^^^^^^^^
105129

106-
error: aborting due to 17 previous errors
130+
error: aborting due to 21 previous errors
107131

0 commit comments

Comments
 (0)