Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix null pointer exception for if-else statement with empty THEN block #2142

Merged
merged 1 commit into from
Jul 23, 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
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)
}
}