From 1f3b4b9223672d28320d7b719a7267f8890b56b6 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 18 Sep 2020 18:16:12 +0900 Subject: [PATCH] Fix false positive `Missing newline after "->"` when `when` entry has if/else block (#916) --- .../ktlint/ruleset/standard/IndentationRule.kt | 3 +-- .../spec/indent/lint-when-expression.kt.spec | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt index cf7f50f667..e708dd33e3 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/IndentationRule.kt @@ -347,8 +347,7 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast { if (lToken != null && lToken.elementType in lTokenSet) { val rElementType = matchingRToken[lToken.elementType] val rToken = lToken.nextSibling { it.elementType == rElementType } - val lf = rToken?.nextLeaf { it.isWhiteSpaceWithNewline() } - return lf?.parent({ it == p }) == null + return rToken?.treeParent == lToken.treeParent } if (nextCodeSibling?.textContains('\n') == false) { return true diff --git a/ktlint-ruleset-standard/src/test/resources/spec/indent/lint-when-expression.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/indent/lint-when-expression.kt.spec index 6a5eb82f3d..1360afcb76 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/indent/lint-when-expression.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/indent/lint-when-expression.kt.spec @@ -19,6 +19,23 @@ fun main() { else -> 2 + 3 } } + val v3 = when (1) { + 1 -> if (true) { + 2 + } else { + 3 + } + else -> 0 + } + + val v4 = when (1) { + 1 -> 1.let { + it + 1 + }.let { + it + 1 + } + else -> 0 + } } // expect