if / else-if chain has no final else clause
ID |
c.maintainability.if_else_chain_without_else |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
C / C++ |
Description
This if / else if chain has no final else, so a value that matches none of the conditions falls through silently. That can hide unhandled inputs and logic errors. Add a final else to handle the remaining cases explicitly, even if only to assert or log.
Rationale
This if / else if chain has no final else, so a value that matches none of the conditions falls through silently. That can hide unhandled inputs and logic errors. Add a final else to handle the remaining cases explicitly, even if only to assert or log.
The following code illustrates the pattern detected by this rule:
void classify(int x) {
// FLAGGED: if / else-if chain has no final else clause
if (x == 1) {
handleOne();
} else if (x == 2) {
handleTwo();
}
}
Remediation
Follow secure coding practices and review the references below for detailed remediation guidance.