Skip to content

Commit

Permalink
Fix null pointer exception for if-else statement with empty THEN block (
Browse files Browse the repository at this point in the history
#2142)

Closes #2135
  • Loading branch information
paul-dingemans authored Jul 23, 2023
1 parent b9eaa21 commit b139c14
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Do not wrap a binary expression after an elvis operator in case the max line length is exceeded ([#2128](https://github.com/pinterest/ktlint/issues/2128))
* Fix indent of IS_EXPRESSION, PREFIX_EXPRESSION and POSTFIX_EXPRESSION in case it contains a linebreak `indent` [#2094](https://github.com/pinterest/ktlint/issues/2094)
* Add new experimental rule `function-literal`. This rule enforces the parameter list of a function literal to be formatted consistently. `function-literal` [#2121](https://github.com/pinterest/ktlint/issues/2121)
* Fix null pointer exception for if-else statement with empty THEN block `if-else-bracing` [#2135](https://github.com/pinterest/ktlint/issues/2135)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class IfElseBracingRule :
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
): Boolean {
emit(
node.firstChildNode.startOffset,
node.firstChildNode?.startOffset ?: node.startOffset,
"All branches of the if statement should be wrapped between braces if at least one branch is wrapped between braces",
true,
)
Expand Down Expand Up @@ -149,15 +149,26 @@ public class IfElseBracingRule :
}
KtBlockExpression(null).apply {
val previousChild = node.firstChildNode
node.replaceChild(node.firstChildNode, this)
if (previousChild == null) {
node.addChild(this, null)
} else {
node.replaceChild(node.firstChildNode, this)
}
addChild(LeafPsiElement(LBRACE, "{"))
addChild(PsiWhiteSpaceImpl(indentConfig.childIndentOf(node)))
if (previousChild != null) {
addChild(PsiWhiteSpaceImpl(indentConfig.childIndentOf(node)))
}
prevLeaves
.dropWhile { it.isWhiteSpace() }
.forEach(::addChild)
addChild(previousChild)
.takeIf { it.isNotEmpty() }
?.forEach(::addChild)
if (previousChild != null) {
addChild(previousChild)
}
nextLeaves.forEach(::addChild)
addChild(PsiWhiteSpaceImpl(node.indent()))
if (previousChild != null) {
addChild(PsiWhiteSpaceImpl(node.indent()))
}
addChild(LeafPsiElement(RBRACE, "}"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,21 @@ class IfElseBracingRuleTest {
.hasNoLintViolations()
}
}

@Test
fun `Issue 2135 - Given ktlint_official code style and an if statement with and empty THEN block then do not throw a null pointer exception`() {
val code =
"""
val foo = if (false) else { bar() }
""".trimIndent()
val formattedCode =
"""
val foo = if (false) {} else { bar() }
""".trimIndent()
@Suppress("ktlint:standard:argument-list-wrapping", "ktlint:standard:max-line-length")
multiLineIfElseRuleAssertThat(code)
.withEditorConfigOverride(CODE_STYLE_PROPERTY to ktlint_official)
.hasLintViolation(1, 22, "All branches of the if statement should be wrapped between braces if at least one branch is wrapped between braces")
.isFormattedAs(formattedCode)
}
}

0 comments on commit b139c14

Please # to comment.