Inspectopedia Help

Unreachable code

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.

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(); }

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.

-UnreachableCode
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 | Probable bugs

New in 2024.1

Inspection options

Here you can find the description of settings available for the Unreachable code inspection, and the reference of their default values.

Ignore trivial exit statements

Default: Selected

Respect 'Constant value' inspection suppressions

Default: 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