Unreachable Type Switch
ID |
go.unreachable_type_switch |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Go |
Tags |
reliability, suspicious |
Description
Reports a type switch that lists the same case type more than once. A value matches the first
case clause whose type it satisfies, so a type repeated in a later case can never be reached.
Rationale
The duplicate clause is dead code — the earlier case always wins — and is almost always a
copy-paste mistake or a case that was meant to name a different type. A clause may list several
types (case int, string:); each listed type is compared against the types already seen, and
the first clause that reintroduces a type is reported. Only exact duplicate types are flagged;
the broader case where an interface case shadows a later, more specific type is out of scope.
switch v.(type) {
case int:
return "int"
case string:
return "string"
case int: // FLAW — int is already handled above, this is unreachable
return "int again"
}