Adjacent cases of a switch have the same body

tx0/go:mergeable-casesstrictwarningstyle

What it finds

case a: f() followed by case b: f(): adjacent cases with the same body can be one case, case a, b: f(), which says that they are handled alike and keeps them alike.

Merging keeps the order in which the expressions are tested, so it changes nothing. Left out are default, bodies that end in fallthrough, which would then run once instead of twice, and type switches, where the bound variable has a different type in each case.

This rule is in the strict tier: listing cases one by one is often deliberate, with a comment for each, and common in generated code. The fix is offered when it removes no comments.

From the rule's source, mergeable_cases.rs.

Example from the rule's tests
package p func f(n int, v any) int {	switch n {	case 1:		return g(n)	case 2, 3:tx0 reports: this case has the same body as the one before it		return g(n)	case 4:tx0 reports: same body		return g(n)	case 5:		return 0	default:		return 0	}	switch {	case n < 0:		n = 0	case n > 9:		n = 1	case n > 99:	case n > 999:	}	switch n {	case 1:		n++		fallthrough	case 2:		n++		fallthrough	case 3:	}	switch v.(type) {	case int:		return 1	case string:		return 1	}	return n} func g(n int) int { return n }
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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:mergeable-cases" = "warning"

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:mergeable-cases] reason="..."

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