Inspectopedia
 
2024.3

Unreachable code

Warning
Last modified: 03 December 2024

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:

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:

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:

New in 2024.1