goto label declared inside a switch body
ID |
c.maintainability.label_inside_switch |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Control Flow |
Language |
C / C++ |
Description
This goto label is declared inside a switch body, mixing two forms of jump-based control flow in the same block. Readers have to track both the case/default dispatch and an independent label, which makes the block harder to follow and easy to misedit. Move the label out of the switch, or restructure the jump as a loop or a helper function.
Rationale
This goto label is declared inside a switch body, mixing two forms of jump-based control flow in the same block. Readers have to track both the case/default dispatch and an independent label, which makes the block harder to follow and easy to misedit. Move the label out of the switch, or restructure the jump as a loop or a helper function.
The following code illustrates the pattern detected by this rule:
void retryLoop(int x) {
switch (x) {
// FLAGGED: goto label declared inside a switch body
retry:
x++;
if (x < 5) {
goto retry;
}
break;
default:
break;
}
}