C-style underflow/overflow checks will panic in debug builds. Using safe methods like checked_add
or overflowing_add
ensures that overflow conditions are explicitly handled, preventing unexpected panics.
Code examples
Noncompliant code example
let a = 1i32;
let b = 2i32;
if a + b < a {
// Noncompliant: This will panic in debug builds
// handle overflow
}
Compliant solution
let a = 1i32;
let b = 2i32;
if a.checked_add(b).is_none() {
// Compliant: Explicitly handle overflow
// handle overflow
}