Unnecessary else after a returning if branch
ID |
c.maintainability.else_after_return |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
C / C++ |
Description
The if branch ends with return, so the following else is unnecessary: its body can be dedented to run unconditionally after the if. Dropping the else flattens the control flow and reads as a guard clause.
Rationale
The if branch ends with return, so the following else is unnecessary: its body can be dedented to run unconditionally after the if. Dropping the else flattens the control flow and reads as a guard clause.
The following code illustrates the pattern detected by this rule:
int classify(int x) {
// FLAGGED: Unnecessary else after a returning if branch
if (x > 0) {
return x;
} else {
return -x;
}
}