ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

If-else brackets compliance

Open pinguinjkeke opened this issue 1 year ago • 1 comments

Expected Rule behavior

If one of if/else/else if use braces then all parts must use it Correct:

if (true) 1 else 2

if (true) 1 else if (false) 2 else 3

if (true) {
    1
} else {
    2
}

if (true) {
    1
} else if (false) {
    2
} else {
    3
}

Incorrect:

if (true) 1 else {
    2
}

if (true) {
    1
} else 2

if (true) {
    1
} else if (false) 2 else {
    3
}

pinguinjkeke avatar Aug 04 '22 09:08 pinguinjkeke

From a perspective of a consistent code style, I do prefer this as well. It however is not documented explicitly as such in the Kotlin Coding Conventions. As of that it does not fit in the standard and experimental ruleset of KtLint. It could be implemented in an additional ruleset.

paul-dingemans avatar Aug 19 '22 19:08 paul-dingemans

Has this been implemented in the last release? I'm currently upgrading detekt in our project to the latest version (1.22.0 which uses ktlint 0.47.1). Now the multiline-if-else rule reports for code snippets like this

if (condition) {
    // ...
} else null

that it is missing braces in the else null line.

obecker avatar Nov 27 '22 13:11 obecker

Has this been implemented in the last release? I'm currently upgrading detekt in our project to the latest version (1.22.0 which uses ktlint 0.47.1). Now the multiline-if-else rule reports for code snippets like this

if (condition) {
    // ...
} else null

that it is missing braces in the else null line.

The behavior of this rules is indeed changed as part of PR #1565 (https://github.com/pinterest/ktlint/pull/1565/files#diff-23102555750e484a7df496237f01c33f59ceca2b0ab415b05de3b05c79c4cd2eR500-R520)

paul-dingemans avatar Nov 28 '22 20:11 paul-dingemans

@paul-dingemans for me it looks like not solved. if (true) 1 else 2 seems incorrect with this ktlint rule

pinguinjkeke avatar Mar 30 '23 15:03 pinguinjkeke

for me it looks like not solved. if (true) 1 else 2 seems incorrect with this ktlint rule

Please be more specific. What exactly seems incorrect? What are your .editorconfig settings?

paul-dingemans avatar Mar 30 '23 16:03 paul-dingemans