KT.COLLAPSIBLE_IF_STATEMENTS

Two 'if' statements can be collapsed

This rule detects if statements which can be collapsed. This can reduce nesting and help improve readability. However, carefully consider whether merging the if statements actually improves readability, as collapsing the statements may hide some edge cases from the reader.

Noncompliant Code

Copy
val i = 1
if (i > 0) {
    if (i < 5) {
        println(i)
    }
}

Compliant Code

Copy
val i = 1
if (i > 0 && i < 5) {
    println(i)
}

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