Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow else-if (..) <statement> as single line construct #2430

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ public class MultiLineIfElseRule :
// Allow single line if statements as long as they are really simple (e.g. do not contain newlines)
// if (...) <statement> // no else statement
// if (...) <statement> else <statement>
return
if (node.treeParent.treeParent.elementType == ELSE) {
// Except in case nested if-else-if on single line
// if (...) <statement> else if (..) <statement>
} else {
return
}
}

Unit
}

emit(node.firstChildNode.startOffset, "Missing { ... }", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,30 @@ class MultiLineIfElseRuleTest {
LintViolation(1, 53, "Missing { ... }"),
).isFormattedAs(formattedCode)
}

@Test
fun `Issue 2420 - Given a single line else-if`() {
val code =
"""
fun foo() {
if (foo) {
doFoo()
} else if (bar) doBar()
}
""".trimIndent()
val formattedCode =
"""
fun foo() {
if (foo) {
doFoo()
} else if (bar) {
doBar()
}
}
""".trimIndent()
multiLineIfElseRuleAssertThat(code)
.hasLintViolations(
LintViolation(4, 21, "Missing { ... }"),
).isFormattedAs(formattedCode)
}
}