(a..b).forEach { } creates a range object where a for loop does not

tx0/kotlin:for-each-over-rangestrictwarningperformance/algorithmicdepth fastperformance

What it finds

for (i in 0 until n) compiles to a plain counting loop. (0 until n).forEach { } allocates the range and iterates it through an iterator.

From the rule's source, for_each_on_range.rs.

Example from the rule's tests
fun run(n: Int) {    (0 until n).forEach { work(it) }tx0 reports: a for loop over the range avoids creating it    (1..n).forEach { work(it) }tx0 reports: a for loop over the range    listOf(1, 2).forEach { work(it) }}
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:for-each-over-range" = "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:for-each-over-range] reason="..."

tx0 explain tx0/kotlin:for-each-over-range shows the same in the terminal. Found a false positive? Report it.