KT.IMPLICIT_UNIT_RETURN_TYPE

Functions using expression statements have an implicit return type

Functions using expression statements have an implicit return type. Changing the type of the expression accidentally, changes the functions return type. This may lead to backward incompatibility. Use a block statement to make clear this function will never return a value.

Noncompliant Code

Copy
fun errorProneUnit() = println("Hello Unit")
fun errorProneUnitWithParam(param: String) = param.run { println(this) }
fun String.errorProneUnitWithReceiver() = run { println(this) }

Compliant Code

Copy
fun blockStatementUnit() {
    // code
}

// explicit Unit is compliant by default; can be configured to enforce block statement
fun safeUnitReturn(): Unit = println("Hello Unit")

Options

  • allowExplicitReturnType (default: True)

    if functions with explicit 'Unit' return type should be allowed

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