Skip to content

Commit

Permalink
Keep arrow when both parameter list and block of function literal are…
Browse files Browse the repository at this point in the history
… empty (pinterest#2469)

Removal of the arrow in this case would lead to a compilation error as the function literal is changed to a normal block when removing the arrow.

Closes pinterest#2465
  • Loading branch information
paul-dingemans authored Dec 29, 2023
1 parent b3c9277 commit 55060c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public class FunctionLiteralRule :
require(arrow.elementType == ARROW)
arrow
.prevSibling { it.elementType == VALUE_PARAMETER_LIST }
?.takeIf { it.findChildByType(VALUE_PARAMETER) == null }
?.takeIf { it.findChildByType(VALUE_PARAMETER) == null && arrow.isFollowedByNonEmptyBlock() }
?.let {
emit(arrow.startOffset, "Arrow is redundant when parameter list is empty", true)
if (autoCorrect) {
Expand All @@ -387,6 +387,11 @@ public class FunctionLiteralRule :
}
}

private fun ASTNode.isFollowedByNonEmptyBlock(): Boolean {
require(elementType == ARROW)
return nextSibling { it.elementType == BLOCK }?.firstChildNode != null
}

private fun visitBlock(
block: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,13 @@ class FunctionLiteralRuleTest {
.hasLintViolation(1, 42, "Arrow is redundant when parameter list is empty")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2465 - Given a function literal without parameters and with an empty block then do not remove the arrow`() {
val code =
"""
fun foo(block: () -> Unit): Unit = foo { -> }
""".trimIndent()
functionLiteralRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 55060c3

Please sign in to comment.