A size or length is compared with 0 in a way that is always true or always false

tx0/java:negative-size-checkdefaultwarningbug

What it finds

list.size() < 0, array.length >= 0: a size or length is never negative, so the first comparison is always false and the second always true. Usually <= 0, > 0 or == 0 was meant.

The size must be .length of an array, length() of a String or StringBuilder, or size() of a variable declared as one of the collections and maps of java.util, with types that follow from this file.

From the rule's source, negative_size_check.rs.

Example from the rule's tests
import java.util.List;import java.util.Map; class Sizes {  boolean f(List<String> list, Map<String, Integer> map, String s, int[] array, Custom custom) {    if (list.size() < 0) {tx0 reports: list.size() < 0 is always false: list.size() is never negative      return true;    }    if (array.length >= 0) {tx0 reports: array.length >= 0 is always true      return true;    }    if (0 > s.length()) {tx0 reports: 0 > s.length() is always false      return true;    }    if (0 <= map.size()) {tx0 reports: is always true      return true;    }    return list.size() <= 0 || array.length > 0 || s.length() == 0 || custom.size() < 0 || custom.length < 0;  }   static class Custom {    int length = -1;     int size() {      return -1;    }  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:negative-size-check" = "off"

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:negative-size-check] reason="..."

tx0 explain tx0/java:negative-size-check shows the same in the terminal. Found a false positive? Report it.