if / else-if chain has no final else clause
ID |
rust.maintainability.if_else_chain_without_else |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
Rust |
Description
Reports an if / else if chain that ends without a final else, leaving the case where none of the conditions holds unhandled.
Rationale
A chain of alternatives reads as an exhaustive classification, but with no final else the remaining case falls through in silence: whatever the variables held before the chain is what survives, and as an expression the chain yields () rather than a value. Whether the omission was deliberate or an oversight is invisible to a reader and to the compiler, so the unhandled input surfaces as a wrong value far from this code.
The following code illustrates the pattern detected by this rule:
fn sign(value: i32) -> i32 {
let mut result = 0;
// FLAGGED: if / else-if chain has no final else clause
if value < 0 {
result = -1;
} else if value > 0 {
result = 1;
}
result
}