A variable declared in one case group of a switch is used in a later group, where it may not have been assigned

tx0/java:switch-group-local-scopestrictwarningmaintainability/designdepth fastsmell

What it finds

A local variable declared under one case is in scope for the rest of the switch, but its initializer runs only when that case is taken. A later case that uses it relies on falling through, or reads a variable it must assign first.

Reported is a use, in a later group, of a local variable declared in a group of a switch with colon labels.

From the rule's source, switch_group_local_scope.rs.

Example from the rule's tests
class Cases {  int run(int a) {    switch (a) {      case 1:        int b = 2;        return b;      case 2:        b = 3;tx0 reports: belongs to an earlier case        return b;      default:        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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:switch-group-local-scope" = "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/java:switch-group-local-scope] reason="..."

tx0 explain tx0/java:switch-group-local-scope shows the same in the terminal. Found a false positive? Report it.