summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/arithmetic_side_effects.txt
blob: 6c7d51a4989e0b34f54d89c444399be3f502e8f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
### What it does
Checks any kind of arithmetic operation of any type.

Operators like `+`, `-`, `*` or `<<` are usually capable of overflowing according to the [Rust
Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
or can panic (`/`, `%`).

Known safe built-in types like `Wrapping` or `Saturing`, floats, operations in constant
environments, allowed types and non-constant operations that won't overflow are ignored.

### Why is this bad?
For integers, overflow will trigger a panic in debug builds or wrap the result in
release mode; division by zero will cause a panic in either mode. As a result, it is
desirable to explicitly call checked, wrapping or saturating arithmetic methods.

#### Example
```
// `n` can be any number, including `i32::MAX`.
fn foo(n: i32) -> i32 {
  n + 1
}
```

Third-party types can also overflow or present unwanted side-effects.

#### Example
```
use rust_decimal::Decimal;
let _n = Decimal::MAX + Decimal::MAX;
```

### Allowed types
Custom allowed types can be specified through the "arithmetic-side-effects-allowed" filter.