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

Fix false -ve on else with single line binary or chained call #2063

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ At this point in time, it is not yet decided what the next steps will be. Ktlint
* Do not merge an annotated expression body with the function signature even if it fits on a single line ([#2043](https://github.com/pinterest/ktlint/issues/2043))
* Ignore property with name `serialVersionUID` in `property-naming` ([#2045](https://github.com/pinterest/ktlint/issues/2045))
* Prevent incorrect reporting of violations in case a nullable function type reference exceeds the maximum line length `parameter-list-wrapping` ([#1324](https://github.com/pinterest/ktlint/issues/1324))
* Prevent false negative on `else` branch when body contains only chained calls or binary expression ([#2057](https://github.com/pinterest/ktlint/issues/2057))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,33 @@ public class MultiLineIfElseRule :
return
}

if (node.elementType == ELSE && node.firstChildNode?.elementType == BINARY_EXPRESSION) {
if (node.elementType == ELSE &&
node.firstChildNode?.elementType == BINARY_EXPRESSION &&
node.firstChildNode.firstChildNode?.elementType == IF
atulgpt marked this conversation as resolved.
Show resolved Hide resolved
) {
// Allow
// val foo = if (bar1) {
// "bar1"
// } else {
// null
// } ?: "something-else"
// val foo2 = if (bar1) {
// "bar1"
// } else if (bar2) {
// null
// } else {
// null
// } ?: "something-else"
return
}

if (node.elementType == ELSE && node.firstChildNode?.elementType == DOT_QUALIFIED_EXPRESSION) {
if (node.elementType == ELSE &&
node.firstChildNode?.elementType == DOT_QUALIFIED_EXPRESSION &&
node.firstChildNode.firstChildNode?.elementType == IF
atulgpt marked this conversation as resolved.
Show resolved Hide resolved
) {
// Allow
// val foo = if (bar1) {
// "bar1"
// } else {
// "bar2"
// }.plus("foo")
// val foo = if (bar1) {
// "bar1"
// } else if (bar2) {
// "bar2"
// } else {
// "bar3"
// }.plus("foo")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,16 @@ class MultiLineIfElseRuleTest {
fun `Issue 1904 - Given an nested if else statement followed by an elvis operator`() {
val code =
"""
val foo = if (bar1) {
val foo1 = if (bar1) {
"bar1"
} else {
null
} ?: "something-else"

val foo2 = if (bar1) {
atulgpt marked this conversation as resolved.
Show resolved Hide resolved
"bar1"
} else if (bar2) {
null
} else {
null
} ?: "something-else"
Expand All @@ -584,11 +592,39 @@ class MultiLineIfElseRuleTest {
}

@Test
fun `Issue 1904 - Given an nested if else statement and else which is part of a dot qualified expression`() {
fun `Issue 2057 - Given an else condition with single line binary expression`() {
val code =
"""
val foo = if (bar1) {
"bar1"
} else bar2 ?: "something-else"
""".trimIndent()
val formattedCode =
"""
val foo = if (bar1) {
"bar1"
} else {
bar2 ?: "something-else"
}
""".trimIndent()
multiLineIfElseRuleAssertThat(code)
.hasLintViolations(
LintViolation(3, 8, "Missing { ... }"),
).isFormattedAs(formattedCode)
}

@Test
fun `Issue 1904 - Given an nested if else statement and else which is part of a dot qualified expression`() {
val code =
"""
val foo1 = if (bar1) {
"bar1"
} else {
"bar2"
}.plus("foo")

val foo2 = if (bar1) {
"bar1"
} else if (bar2) {
"bar2"
} else {
Expand All @@ -597,4 +633,49 @@ class MultiLineIfElseRuleTest {
""".trimIndent()
multiLineIfElseRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2057 - Given an else with chained condition`() {
val code =
"""
val foo = if (System.currentTimeMillis() % 2 == 0L) {
0
} else System.currentTimeMillis().toInt()
""".trimIndent()
val formattedCode =
"""
val foo = if (System.currentTimeMillis() % 2 == 0L) {
0
} else {
System.currentTimeMillis().toInt()
}
""".trimIndent()
multiLineIfElseRuleAssertThat(code)
.hasLintViolations(
LintViolation(3, 8, "Missing { ... }"),
).isFormattedAs(formattedCode)
}

@Test
fun `Issue 2057 - Given an if with chained condition`() {
val code =
"""
val foo = if (System.currentTimeMillis() % 2 == 0L) System.currentTimeMillis().toInt()
else {
System.currentTimeMillis().toInt()
}
""".trimIndent()
val formattedCode =
"""
val foo = if (System.currentTimeMillis() % 2 == 0L) {
System.currentTimeMillis().toInt()
} else {
System.currentTimeMillis().toInt()
}
""".trimIndent()
multiLineIfElseRuleAssertThat(code)
.hasLintViolations(
LintViolation(1, 53, "Missing { ... }"),
).isFormattedAs(formattedCode)
}
}