KT.RETURN_FROM_FINALLY

Return statement in finally blocks

Reports all return statements in finally blocks. Using return statements in finally blocks can discard and hide exceptions that are thrown in the try block. Furthermore, this rule reports values from finally blocks, if the corresponding try is used as an expression.

Noncompliant Code

Copy
fun foo() {
    try {
        throw MyException()
    } finally {
        return // prevents MyException from being propagated
    }
}

val a: String = try { "s" } catch (e: Exception) { "e" } finally { "f" }

Options

  • ignoreLabeled (default: False)

    ignores labeled return statements

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html