From b139c14dcf4c9b97d063b55a33504872da860cc8 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Sun, 23 Jul 2023 14:43:56 +0200 Subject: [PATCH] Fix null pointer exception for if-else statement with empty THEN block (#2142) Closes #2135 --- CHANGELOG.md | 1 + .../standard/rules/IfElseBracingRule.kt | 23 ++++++++++++++----- .../standard/rules/IfElseBracingRuleTest.kt | 17 ++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ff2bd460..2770232846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRule.kt index a88ba9600f..1cfda745e1 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRule.kt @@ -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, ) @@ -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, "}")) } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRuleTest.kt index b3fe8f0069..99bbddc1c8 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IfElseBracingRuleTest.kt @@ -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) + } }