switch repeats a case value, so the later branch is dead
ID |
dart.correctness.no_duplicate_case_values |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
Dart |
Rationale
A switch dispatches to the first clause whose value matches, so the second occurrence can never be reached and its body is dead code. The duplicate is almost always a copy-paste of a clause whose value was meant to be changed, which means the value the author intended to handle is not handled at all and silently falls through to the default clause, or out of the switch entirely. Nothing at runtime distinguishes that from a deliberate fallback.
The following code illustrates the pattern detected by this rule:
class Router {
String byCode(int code) {
// FLAGGED: switch repeats a case value, so the later branch is dead
switch (code) {
case 200:
return 'ok';
case 404:
return 'missing';
case 200:
return 'still ok';
default:
return 'other';
}
}
}