mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
37 lines
1.4 KiB
HTML
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" -> foo();
|
|
case "bar" -> 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> |