Files
openide/java/java-impl/resources/inspectionDescriptions/SwitchStatementWithTooFewBranches.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

37 lines
1.4 KiB
HTML

<html>
<body>
Reports <code>switch</code> statements and expressions with too few <code>case</code> labels, and suggests rewriting them as <code>if</code>
and <code>else if</code> statements.
<p>Example (minimum branches == 3):</p>
<pre><code>
switch (expression) {
case "foo" -&gt; foo();
case "bar" -&gt; bar();
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
if ("foo".equals(expression)) {
foo();
} else if ("bar".equals(expression)) {
bar();
}
</code></pre>
<p>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 <code>switch</code> is converted to <code>if</code>
which might be undesired.
</p>
<!-- tooltip end -->
<p>Configure the inspection:</p>
<p>Use the <b>Minimum number of branches</b> field to specify the minimum expected number of <code>case</code> labels.</p>
<p>Use the <b>Do not report pattern switch statements</b> option to avoid reporting switch statements and expressions that
have pattern branches. E.g.:</p>
<pre><code>
String result = switch(obj) {
case String str -> str.trim();
default -> "none";
};
</code></pre>
<p>It might be preferred to keep the switch even with a single pattern branch, rather than using the <code>instanceof</code> statement.</p>
</body>
</html>