Skip to content

Commit

Permalink
Ignore function which is returned as result in a function body (#2526)
Browse files Browse the repository at this point in the history
Closes #2521
  • Loading branch information
paul-dingemans authored Feb 3, 2024
1 parent 9ab5f9e commit 4c5c305
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_DECLARATION
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.WHEN
import com.pinterest.ktlint.rule.engine.core.api.Rule
import com.pinterest.ktlint.rule.engine.core.api.RuleId
Expand Down Expand Up @@ -120,7 +121,9 @@ public class BlankLineBeforeDeclarationRule :
return
}

if (node.elementType == FUN && node.prevCodeSibling()?.elementType == EQ) {
if (node.elementType == FUN &&
(node.prevCodeSibling()?.elementType == EQ || node.prevCodeSibling()?.elementType == RETURN_KEYWORD)
) {
// Allow:
// val foo =
// fun(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,17 @@ class BlankLineBeforeDeclarationRuleTest {
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2521 - Given a function returning a function in a block body`() {
val code =
"""
fun foo(bar: Int): (Int) -> Int {
return fun(baz: Int): Int {
return bar + baz
}
}
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 4c5c305

Please sign in to comment.