A case of a switch repeats an earlier one

tx0/go:duplicate-casedefaultwarningbug

What it finds

case a, b, a:: a case expression that repeats an earlier one of the same switch never matches, because the earlier one is tested first and matches the same values.

The compiler already rejects duplicate constants, so what is left are expressions with variables, which are just as wrong. Case expressions are evaluated in order until one matches, so the comparison stops at the first one that receives or calls anything but len and cap, since it could change what later ones read. Type switches are left to the compiler.

From the rule's source, duplicate_case.rs.

Example from the rule's tests
package p import "os" func f(x, a, b int, s string, v any) int {	switch x {	case a, b:		return 1	case a:tx0 reports: a is already a case of this switch		return 2	}	switch s {	case os.Args[0], os.Args[1], (os.Args[0]):tx0 reports: os.Args[0] is already a case	}	switch {	case x > a:	case x == b:	case x > a:tx0 reports: x > a is already a case	case len(s) > 1:	case len(s) > 1:tx0 reports: len(s) > 1 is already a case	}	switch x {	case next():	case a:	case a:	}	switch {	case a > 0:		if a > 0 {		}	case b > 0:	}	switch v.(type) {	case int:	case string:	}	return 0} func next() int { return 0 }
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:duplicate-case" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:duplicate-case] reason="..."

tx0 explain tx0/go:duplicate-case shows the same in the terminal. Found a false positive? Report it.