Inspectopedia
 
2024.3

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

Warning
New
Last modified: 03 December 2024

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:

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