The condition of an assert changes state, which happens only when assertions are enabled

tx0/java:assert-side-effectrecommendedwarningbug

What it finds

assert queue.remove(item); removes the item only when assertions are enabled, which they are not by default in production: the program then behaves differently with and without -ea. The work belongs before the assert, which only checks its result.

Reported are assignments, ++ and --, and calls of methods whose names say they change something (add, remove, put, poll, offer, set, clear, delete, create, write, insert, push, pop), in the condition of an assert. assert enabled = true;, which finds out whether assertions are enabled, is left alone.

From the rule's source, assert_side_effect.rs.

Example from the rule's tests
import java.util.List; class Jobs {  int seen;   void take(List<String> queue, String item) {    assert queue.remove(item);tx0 reports: this runs only when assertions are enabled    assert seen++ < 10;tx0 reports: this runs only when assertions are enabled    assert queue.contains(item);    boolean enabled = false;    assert enabled = true;    assert queue.stream().noneMatch(s -> s.isEmpty());  }}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:assert-side-effect" = "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:assert-side-effect] reason="..."

tx0 explain tx0/java:assert-side-effect shows the same in the terminal. Found a false positive? Report it.