Inspectopedia Help

Use of Optional.ofNullable with null or not-null argument

Reports uses of Optional.ofNullable() where always null or always not-null argument is passed. There's no point in using Optional.ofNullable() in this case: either Optional.empty() or Optional.of() should be used to explicitly state the intent of creating an always-empty or always non-empty optional respectively. It's also possible that there's a mistake in Optional.ofNullable() argument, so it should be examined.

Example:

Optional<String> empty = Optional.ofNullable(null); // should be Optional.empty(); Optional<String> present = Optional.ofNullable("value"); // should be Optional.of("value");

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.

OptionalOfNullableMisuse
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

This inspection depends on the Java feature 'Stream and Optional API' which is available since Java 8.

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024