A string variable is extended with += inside a loop, which copies the whole string each time

tx0/kotlin:string-grown-in-looprecommendedwarningperformance/allocationdepth fastperformance

What it finds

Strings are immutable: text += part makes a new string with all of text copied, so building a string this way in a loop takes time quadratic in its length. buildString or a StringBuilder appends in place. Reported for local vars declared as String or initialized with a string literal.

From the rule's source, string_concat_in_loop.rs.

Example from the rule's tests
fun join(items: List<String>): String {    var text = ""    for (item in items) {        text += itemtx0 reports: text is copied in full on every pass of the loop    }    var other: String = "start"    items.forEach { other += it }tx0 reports: other is copied in full    var count = 0    for (item in items) {        count += item.length    }    var once = ""    once += "x"    return text + other + count + once}
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/kotlin:string-grown-in-loop" = "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:string-grown-in-loop] reason="..."

tx0 explain tx0/kotlin:string-grown-in-loop shows the same in the terminal. Found a false positive? Report it.