Skip to content

function-expression-body

Kotlinstyleno fixdefault: offkotlin/ktlint

Reports a function with a block body that is only a return, which the formatter would write as an expression body (fun f(): Int { return 1 } is fun f(): Int = 1).

A function with a single return as its body is clearer as an expression: the type is inferred and a line of braces goes away. One style makes functions of that kind look the same.

To fix, run the formatter, or write the expression body.

Reported
...
fun foo(a: Int, b: String, c: Long, d: Double, e: Float, f: Short, g: Byte, h: Char, i: Boolean, j: Unit): Int { return 1 }
...
1
...
fun ok(a: Int) = a
fun sum(): Int { return 1 }
}
kotlin:
lint:
rules:
function-expression-body: warn # off (default), warn, error

Enabled by the presets: kotlin/ktlint.

Suppress one line with sudsy-disable-next-line function-expression-body in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ktlint function-expression-body full