ktlint
ktlint copied to clipboard
If-else brackets compliance
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
}
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.
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.
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 thisif (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 for me it looks like not solved. if (true) 1 else 2
seems incorrect with this ktlint rule
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?