if and else branches have identical bodies
ID |
c.maintainability.useless_control_flow |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Redundancy |
Language |
C / C++ |
Description
The if and else branches execute the same statement, so the condition has no effect on the outcome. This is usually a copy-paste bug (one branch should differ) or leftover code. Fix the branch that should differ, or drop the redundant if/else and run the statement unconditionally.
Rationale
The if and else branches execute the same statement, so the condition has no effect on the outcome. This is usually a copy-paste bug (one branch should differ) or leftover code. Fix the branch that should differ, or drop the redundant if/else and run the statement unconditionally.
The following code illustrates the pattern detected by this rule:
int adjust(int c, int x) {
// FLAGGED: if and else branches have identical bodies
if (c)
x = x + 1;
else
x = x + 1;
return x;
}