A method hands back an Optional around a collection, map or array
What it finds
An empty collection already says that there is nothing, so Optional<List<Order>> gives callers two ways to express absence and two checks to make. Returning an empty list is simpler.
Reported is a method whose return type is Optional of a List, Set, Map, Collection or an array.
From the rule's source, optional_of_collection.rs.
import java.util.List;import java.util.Optional; class Orders { Optional<List<String>> find() {tx0 reports: an empty collection already expresses absence return Optional.empty(); } Optional<String> one() { return Optional.empty(); }} 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-wrapped-container" = "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-wrapped-container] reason="..."tx0 explain tx0/java:optional-wrapped-container shows the same in the terminal. Found a false positive? Report it.