A switch statement lacks a default, has it before other labels, or has fewer than three labels

tx0/java:switch-shapestrictwarningstyle

What it finds

Three shapes of switch statement that read worse than they need to: one without a default label, where a value nobody thought of passes silently; one whose default is not the last label, where readers look for it at the end; and one with fewer than three labels, which an if says more directly.

The default check applies only to switch statements: switch expressions must cover every value anyway, and so must statements whose labels are patterns or null.

From the rule's source, switch_shape.rs.

Example from the rule's tests
class Days {  String name(int d) {    switch (d) {tx0 reports: this switch has no default label      case 1:        return "one";      case 2:        return "two";      case 3:        return "three";    }    switch (d) {tx0 reports: the default label is not the last one      default:        return "?";      case 1:        return "one";      case 2:        return "two";    }  }   void shape(Object o) {    switch (o) {      case Integer i -> System.out.println(i);      case String s -> System.out.println(s);      case null -> System.out.println("none");      case Object other -> System.out.println(other);    }  }   void tiny(int d) {    switch (d) {tx0 reports: fewer than three labels      case 1:        break;      default:        break;    }  }}
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-shape" = "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-shape] reason="..."

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