Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::implicit_saturating_sub: Checks for implicit saturating subtraction
What it meansChecks for implicit saturating subtraction. It is in the style group and warns by default.
Does it matter?
Simplicity and readability. Instead we can easily use an builtin function.
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let mut i: u32 = end - start;
if i != 0 {
i -= 1;
}
Write this instead
let mut i: u32 = end - start;
i = i.saturating_sub(1);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::implicit_saturating_sub)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
implicit_saturating_sub = "allow"One run
cargo clippy -- -A clippy::implicit-saturating-subThe [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.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for implicit saturating subtraction.
Why is this bad?
Simplicity and readability. Instead we can easily use an builtin function.
Example
let mut i: u32 = end - start;
if i != 0 {
i -= 1;
}
Use instead:
let mut i: u32 = end - start;
i = i.saturating_sub(1);
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- get_firstaccessing first element with `x.get(0)`
- if_same_then_elseChecks for if/else with the same body as the then part and the else part
- implicit_saturating_addmanual saturating add detected
- inconsistent_digit_groupingdigits grouped inconsistently by underscores
- infallible_destructuring_matchyou seem to be trying to use `match` to destructure a single infallible pattern
- inherent_to_stringimplementation of inherent method `to_string(&self) -> String` for type `main::A`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?