Skip to content

Commit

Permalink
Fix false positive when anonymous function is used as value argument (#…
Browse files Browse the repository at this point in the history
…2718)

Closes #2694
  • Loading branch information
paul-dingemans authored Jun 25, 2024
1 parent 9d9ad58 commit 2ecb753
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 2ecb753

Please sign in to comment.