Example:
void finishApplication() {
System.exit(0);
System.out.println("Application is terminated"); // Unreachable code
}
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:
void run() {
performAction();
System.out.println("Action is performed"); // Unreachable code
}
static void performAction() {
throw new AssertionError();
}
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 @org.jetbrains.annotations.Contract
annotation from
org.jetbrains:annotations
package:
void run() {
performAction();
System.out.println("Action is performed"); // No warning anymore
}
@Contract("-> _") // implementation will be replaced
static void performAction() {
throw new AssertionError();
}
New in 2024.1