Emptiness is tested by comparing a size or length with 0 or by equality with an empty string
What it finds
list.size() == 0, name.length() > 0 and name.equals("") ask whether something is empty the long way round; isEmpty() says it directly, and for some collections it is also cheaper than counting.
Reported is size() or length() without arguments compared with 0 by ==, !=, > or <=, or with 1 by < or >=, and equals("") or "".equals(x) on strings.
From the rule's source, size_compared_for_emptiness.rs.
import java.util.List; class Checks { boolean check(List<String> items, String name) { boolean a = items.size() == 0;tx0 reports: tests for emptiness boolean b = name.length() > 0;tx0 reports: tests for emptiness boolean c = 0 < items.size();tx0 reports: tests for emptiness boolean d = name.equals("");tx0 reports: tests for emptiness boolean e = items.size() > 1; boolean f = items.isEmpty(); return a || b || c || d || e || f; }} 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:size-compared-for-emptiness" = "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:size-compared-for-emptiness] reason="..."tx0 explain tx0/java:size-compared-for-emptiness shows the same in the terminal. Found a false positive? Report it.