rust-clippy
rust-clippy copied to clipboard
int_plus_one changes program behavior in regards of overflows
Summary
int_plus_one
suggestions prevent overflows from happening because it removes a potential case where we could run into T::max() + 1
Reproducer
I tried this code:
fn main() {
dbg!(comp(254));
dbg!(comp(255)); // overflow/panic
dbg!(comp_clippyfixed(254));
dbg!(comp_clippyfixed(255)); // no panic
}
fn comp(x: u8) -> bool {
1 >= x + 1
}
fn comp_clippyfixed(x: u8) -> bool {
1 > x
}
Version
rustc 1.64.0-nightly (0f4bcadb4 2022-07-30)
binary: rustc
commit-hash: 0f4bcadb46006bc484dad85616b484f93879ca4e
commit-date: 2022-07-30
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6
Additional Labels
No response
If the overflow check is needed it should be using checked_add
. The docs could be updated to note this.
I agree. checked_add
and wrapping_add
should be used instead, for explicitness. When people use bare ops, they usually don't think about overflows