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

tx0/kotlin:negative-size-checkstrictwarningreliability/arithmeticdepth fastbug

What it finds

The size of a collection, an array or a string is never negative, so list.size < 0 is always false and text.length >= 0 always true. Such a test was usually meant to be == 0, > 0 or isEmpty(), and the code it guards never runs or always runs.

Reported for size, length and count() compared with 0 when the receiver is known to be a collection, an array, a map or a string: from its declaration, a factory such as listOf, a string literal, or a chain of collection operations. A property of the same name in another class, which may well be negative, is left alone.

From the rule's source, negative_size_check.rs.

Example from the rule's tests
fun check(items: List<String>, name: String, size: Int, box: Box) {    if (items.size < 0) returntx0 reports: items.size < 0 is always false    if (name.length >= 0) println(name)tx0 reports: is always true    if (0 > items.count()) returntx0 reports: is always false    if (listOf(1).size >= 0) println()tx0 reports: is always true    if (items.size <= 0) return    if (size < 0) return    if (box.size < 0) return    if (items.size > 0) println(items)}
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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:negative-size-check" = "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/kotlin:negative-size-check] reason="..."

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