Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::arithmetic_side_effects: arithmetic operation that can potentially result in unexpected side-effects
What it meansChecks 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. It is in the restriction group and off by default.
What you see
warning: arithmetic operation that can potentially result in unexpected side-effects --> main.rs:5:5 | 5 | n + 1 | ^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#arithmetic_side_effects = note: requested on the command line with `-W clippy::arithmetic-side-effects`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
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;
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
// `n` can be any number, including `i32::MAX`.
fn foo(n: i32) -> i32 {
n + 1
}
Write this instead
use rust_decimal::Decimal;
let _n = Decimal::MAX + Decimal::MAX;
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
arithmetic_side_effects = "warn"In code
#![warn(clippy::arithmetic_side_effects)]One run
cargo clippy -- -W clippy::arithmetic-side-effectsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
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: [])
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- allow_attributes#[allow] attribute found
- allow_attributes_without_reason`allow` attribute without specifying a reason
- arbitrary_source_item_orderingincorrect ordering of items (must be alphabetically ordered)
- as_conversionsChecks for usage of as conversions. Note that this lint is specialized in linting every single use of as regardless of whether good alternatives exist or not
- as_pointer_underscoreusing inferred pointer cast
- as_underscoreusing `as _` conversion
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?