The body of a loop always ends with return, break or throw, so it runs at most once

tx0/kotlin:loop-ends-in-first-passrecommendedwarningreliability/control-flowdepth fastbug

What it finds

A loop whose body ends with an unconditional return, break or throw, and has no continue, never reaches a second pass. Either the jump is meant to be conditional or the loop is an if in disguise.

From the rule's source, loop_exits_first_pass.rs.

Example from the rule's tests
fun first(items: List<Int>): Int? {    for (item in items) {        log(item)        return itemtx0 reports: the for loop always stops here in its first pass    }    while (true) {        val line = read() ?: continue        return line.length    }    for (item in items) {        if (item > 0) return item    }    while (ready()) {        work()        breaktx0 reports: the while loop always stops here    }    return null}
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:loop-ends-in-first-pass" = "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:loop-ends-in-first-pass] reason="..."

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