RS.CLIPPY.ARITHMETIC_SIDE_EFFECTS
Any arithmetic expression that can cause side effects like overflows or panics
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,
or can panic (/, %).
Known safe built-in types like Wrapping or Saturating, floats, operations in constant
environments, allowed types and non-constant operations that won't overflow are ignored.
Why restrict this?
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;
Past names
- integer_arithmetic
Configuration
arithmetic-side-effects-allowed: Suppress checking of the passed type names in all types of operations.
If a specific operation is desired, consider using arithmetic_side_effects_allowed_binary or arithmetic_side_effects_allowed_unary instead.
Example
arithmetic-side-effects-allowed = ["SomeType", "AnotherType"]
Noteworthy
A type, say SomeType, listed in this configuration has the same behavior of
["SomeType" , "*"], ["*", "SomeType"] in arithmetic_side_effects_allowed_binary.
(default: [])
arithmetic-side-effects-allowed-binary: Suppress checking of the passed type pair names in binary operations like addition or multiplication.
Supports the "*" wildcard to indicate that a certain type won't trigger the lint regardless
of the involved counterpart. For example, ["SomeType", "*"] or ["*", "AnotherType"].
Pairs are asymmetric, which means that ["SomeType", "AnotherType"] is not the same as
["AnotherType", "SomeType"].
Example
arithmetic-side-effects-allowed-binary = [["SomeType" , "f32"], ["AnotherType", "*"]]
(default: [])
arithmetic-side-effects-allowed-unary: Suppress checking of the passed type names in unary operations like "negation" (-).
Example
arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"]
(default: [])