KT.RETURN_COUNT

Method exceed the number of 'return' statements

Restrict the number of return methods allowed in methods. Having many exit points in a function can be confusing and impacts readability of the code.

Noncompliant Code

Copy
fun foo(i: Int): String {
    when (i) {
        1 -> return "one"
        2 -> return "two"
        else -> return "other"
    }
}

Compliant Code

Copy
fun foo(i: Int): String {
    return when (i) {
        1 -> "one"
        2 -> "two"
        else -> "other"
    }
}

Options

  • max (default: 2)

    define the maximum number of return statements allowed per function

  • excludedFunctions (default: equals)

    define functions to be ignored by this check

  • excludeLabeled (default: False)

    if labeled return statements should be ignored

  • excludeReturnFromLambda (default: True)

    if labeled return from a lambda should be ignored

  • excludeGuardClauses (default: False)

    if true guard clauses at the beginning of a method should be ignored

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