KT.MANDATORY_BRACES_LOOPS

Multi-line loop which do not have braces

This rule detects multi-line for and while loops which do not have braces. Adding braces would improve readability and avoid possible errors.

Noncompliant Code

Copy
for (i in 0..10)
    println(i)

while (true)
    println("Hello, world")

do
    println("Hello, world")
while (true)

Compliant Code

Copy
for (i in 0..10) {
    println(i)
}

for (i in 0..10) println(i)

while (true) {
    println("Hello, world")
}

while (true) println("Hello, world")

do {
    println("Hello, world")
} while (true)

do println("Hello, world") while (true)

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