Inspectopedia Help

Minimum 'switch' branches

Reports switch statements and expressions with too few case labels, and suggests rewriting them as if and else if statements.

Example (minimum branches == 3):

switch (expression) { case "foo" -> foo(); case "bar" -> bar(); }

After the quick-fix is applied:

if ("foo".equals(expression)) { foo(); } else if ("bar".equals(expression)) { bar(); }

Exhaustive switch expressions (Java 14+) or pattern switch statements (Java 17 preview) without the 'default' branch are not reported. That's because compile-time exhaustiveness check will be lost when the switch is converted to if which might be undesired.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

SwitchStatementWithTooFewBranches
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Control flow issues

Configure the inspection:

Use the Minimum number of branches field to specify the minimum expected number of case labels.

Use the Do not report pattern switch statements option to avoid reporting switch statements and expressions that have pattern branches. E.g.:

String result = switch(obj) { case String str -> str.trim(); default -> "none"; };

It might be preferred to keep the switch even with a single pattern branch, rather than using the instanceof statement.

Inspection options

Here you can find the description of settings available for the Minimum 'switch' branches inspection, and the reference of their default values.

Minimum number of branches

2

Do not report pattern switch statements

Not selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

Java, 242.22892

Last modified: 11 September 2024