From 2ecb7537676e9bb6d9c104a8998c12f9e89a9ab3 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 25 Jun 2024 17:09:06 +0200 Subject: [PATCH] Fix false positive when anonymous function is used as value argument (#2718) Closes #2694 --- .../rules/BlankLineBeforeDeclarationRule.kt | 7 +++++++ .../rules/BlankLineBeforeDeclarationRuleTest.kt | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt index 932c980720..53365e5b2f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRule.kt @@ -14,6 +14,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_LITERAL import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY_ACCESSOR import com.pinterest.ktlint.rule.engine.core.api.ElementType.RETURN_KEYWORD +import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHEN import com.pinterest.ktlint.rule.engine.core.api.Rule import com.pinterest.ktlint.rule.engine.core.api.RuleId @@ -132,6 +133,12 @@ public class BlankLineBeforeDeclarationRule : return } + if (node.elementType == FUN && node.treeParent.elementType == VALUE_ARGUMENT) { + // Allow: + // val foo1 = foo2(fun() = 42) + return + } + if (node.elementType == OBJECT_DECLARATION && node.treeParent.elementType == OBJECT_LITERAL) { // Allow: // fun foo() = diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt index e85a1a9094..236922089a 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BlankLineBeforeDeclarationRuleTest.kt @@ -506,4 +506,18 @@ class BlankLineBeforeDeclarationRuleTest { """.trimIndent() blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2694 - Given a call expression with an anonymous function as argument`() { + val code = + """ + val foo1 = + foo( + fun() = 42, + ) + + val foo2 = foo(fun() = 42) + """.trimIndent() + blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations() + } }