An Optional is compared with null
What it finds
if (result == null) where result is an Optional: an Optional stands for a value that may be absent so that it is never null itself. Comparing it with null usually means isEmpty() or isPresent() was meant, or that something returns null where it should return Optional.empty().
The variable must be declared in this file as Optional, OptionalInt, OptionalLong or OptionalDouble of java.util. Code that uses null on purpose as a third state, as a cache does for a value not yet looked up, or that checks a parameter callers might pass as null, is not wrong, so the rule is in the strict tier.
From the rule's source, optional_null_comparison.rs.
import java.util.Optional;import java.util.OptionalInt; class Optionals { Optional<String> cached; boolean f(Optional<String> name, OptionalInt count, String plain) { if (name == null) {tx0 reports: name is an Optional, which should never be null return false; } if (null != count) {tx0 reports: count is an OptionalInt, which should never be null return true; } return cached != null || plain == null || name.isPresent();tx0 reports: cached is an Optional }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:optional-null-comparison" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:optional-null-comparison] reason="..."tx0 explain tx0/java:optional-null-comparison shows the same in the terminal. Found a false positive? Report it.