A finally block returns or jumps, which discards an exception in flight

tx0/kotlin:return-in-finallyrecommendedwarningreliability/error-handlingdepth fastbug

What it finds

return, break or continue in a finally block replaces whatever the try was doing: an exception thrown in the try is silently dropped, and a value it returned is replaced. Jumps that stay inside a lambda, local function or loop within the finally block are left alone.

From the rule's source, jump_in_finally.rs.

Example from the rule's tests
fun read(): Int {    try {        return load()    } finally {        cleanup()        return -1tx0 reports: return in finally discards any exception    }} fun loop() {    for (i in 0..3) {        try {            work()        } finally {            for (j in 0..3) {                if (j == 1) break            }            listOf(1).forEach { return@forEach }            continuetx0 reports: continue in finally discards        }    }}
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:return-in-finally" = "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:return-in-finally] reason="..."

tx0 explain tx0/kotlin:return-in-finally shows the same in the terminal. Found a false positive? Report it.