Skip to content

Commit

Permalink
Do not replace function body with multiple exit points (#2273)
Browse files Browse the repository at this point in the history
* Do not replace function body having a return statement only in case the return statement contains an intermediate exit point

Closes #2269
  • Loading branch information
paul-dingemans authored Sep 23, 2023
1 parent b135fe8 commit e2a6cd2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Fix indent of multiline object declaration inside class `indent` [#2257](https://github.com/pinterest/ktlint/issue/2257)
* Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Do not replace function body having a return statement only in case the return statement contains an intermediate exit point 'function-expression-body' [#2269](https://github.com/pinterest/ktlint/issue/2269)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.leavesIncludingSelf
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
import com.pinterest.ktlint.rule.engine.core.api.prevSibling
import com.pinterest.ktlint.ruleset.standard.StandardRule
Expand Down Expand Up @@ -109,6 +111,7 @@ public class FunctionExpressionBodyRule :
require(block.elementType == BLOCK)
block
.takeIf { it.containingOnly(RETURN) }
?.takeUnless { it.containsMultipleReturns() }
?.findChildByType(RETURN)
?.findChildByType(RETURN_KEYWORD)
?.nextSibling { !it.isWhiteSpace() }
Expand Down Expand Up @@ -161,6 +164,9 @@ public class FunctionExpressionBodyRule :
.singleOrNull()
?.elementType

private fun ASTNode.containsMultipleReturns() =
firstChildLeafOrSelf().leavesIncludingSelf().count { it.elementType == RETURN_KEYWORD } > 1

private fun ASTNode.createUnitTypeReference() =
PsiFileFactory
.getInstance(psi.project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,19 @@ class FunctionExpressionBodyRuleTest {
.isFormattedAs(formattedCode)
.hasLintViolation(1, 16, "Function body should be replaced with body expression")
}

@Test
fun `Given a function with a single expression but having multiple return expression inside then do not covert as it results in a compilation error`() {
val code =
"""
fun foo(): Any {
return if (true) {
Foo()
} else {
return Bar()
}
}
""".trimIndent()
functionExpressionBodyRule(code).hasNoLintViolations()
}
}

0 comments on commit e2a6cd2

Please sign in to comment.