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

50 lines
1.8 KiB
HTML

<html>
<body>
Reports the code which is never reached according to data flow analysis.
It can be the result of previous always-true or always-false condition, unreachable loop body or
catch section. Usually (though not always) unreachable code is a consequence of a previous warning,
so check inspection warnings form "Nullability and data flow problems", "Constant values", or
"Redundant operation on empty container" to better understand the cause.
<p>Example:</p>
<pre><code>
void finishApplication() {
System.exit(0);
System.out.println("Application is terminated"); // Unreachable code
}
</code></pre>
<p>
Note that this inspection relies on method contract inference. In particular, if you call a static or final method
that always throws an exception, then the "always failing" contract will be inferred, and code after the method call
will be considered unreachable. For example:
</p>
<pre><code>
void run() {
performAction();
System.out.println("Action is performed"); // Unreachable code
}
static void performAction() {
throw new AssertionError();
}
</code></pre>
<p>
This may cause false-positives if any kind of code postprocessing is used, for example, if an annotation processor
later replaces the method body with something useful. To avoid false-positive warnings, suppress the automatic
contract inference with explicit <code>@org.jetbrains.annotations.Contract</code> annotation from
<code>org.jetbrains:annotations</code> package:
</p>
<pre><code>
void run() {
performAction();
System.out.println("Action is performed"); // No warning anymore
}
@Contract("-> _") // implementation will be replaced
static void performAction() {
throw new AssertionError();
}
</code></pre>
<p><small>New in 2024.1</small></p>
</body>
</html>