]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/arithmetic_side_effects.txt
Rollup merge of #101389 - lukaslueg:rcgetmutdocs, r=m-ou-se
[rust.git] / src / tools / clippy / src / docs / arithmetic_side_effects.txt
1 ### What it does
2 Checks any kind of arithmetic operation of any type.
3
4 Operators like `+`, `-`, `*` or `<<` are usually capable of overflowing according to the [Rust
5 Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow),
6 or can panic (`/`, `%`).
7
8 Known safe built-in types like `Wrapping` or `Saturing`, floats, operations in constant
9 environments, allowed types and non-constant operations that won't overflow are ignored.
10
11 ### Why is this bad?
12 For integers, overflow will trigger a panic in debug builds or wrap the result in
13 release mode; division by zero will cause a panic in either mode. As a result, it is
14 desirable to explicitly call checked, wrapping or saturating arithmetic methods.
15
16 #### Example
17 ```
18 // `n` can be any number, including `i32::MAX`.
19 fn foo(n: i32) -> i32 {
20   n + 1
21 }
22 ```
23
24 Third-party types can also overflow or present unwanted side-effects.
25
26 #### Example
27 ```
28 use rust_decimal::Decimal;
29 let _n = Decimal::MAX + Decimal::MAX;
30 ```
31
32 ### Allowed types
33 Custom allowed types can be specified through the "arithmetic-side-effects-allowed" filter.