A collection is passed to its own addAll, removeAll, retainAll or containsAll

tx0/kotlin:self-bulk-operationstrictwarningreliability/logicdepth fastbug

What it finds

items.removeAll(items) empties the collection, items.retainAll(items) and items.containsAll(items) do nothing but return a constant, and items.addAll(items) doubles the elements, or for some collections throws ConcurrentModificationException while it iterates over itself. Each is almost always a slip for another collection.

Reported when the receiver and the argument are the same name or chain of names, with no call in them that could give a different collection each time.

From the rule's source, self_bulk_operation.rs.

Example from the rule's tests
fun merge(items: MutableList<Int>, other: MutableList<Int>, holder: Holder) {    items.removeAll(items)tx0 reports: items.removeAll(items) passes the collection to its own removeAll, which empties it    holder.list.addAll(holder.list)tx0 reports: adds every element a second time    items.containsAll(items)tx0 reports: is always true    items.addAll(other)    holder.next().addAll(holder.next())    items.removeAll { it > 3 }}
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:self-bulk-operation" = "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:self-bulk-operation] reason="..."

tx0 explain tx0/kotlin:self-bulk-operation shows the same in the terminal. Found a false positive? Report it.