A range for adds or removes elements of the container it iterates, which invalidates its iterators

tx0/cpp:container-changed-in-range-forrecommendederrorreliability/collectionsdepth fastbug

What it finds

A range for keeps iterators into the container; push_back, insert, erase, clear and the like may reallocate or unlink the element the loop stands on, which is undefined behaviour. Loops that leave right after the change, with break, return, throw or goto, are left alone.

From the rule's source, container_changed_in_range_for.rs.

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/cpp:container-changed-in-range-for" = "error"

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/cpp:container-changed-in-range-for] reason="..."

tx0 explain tx0/cpp:container-changed-in-range-for shows the same in the terminal. Found a false positive? Report it.