Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not replace function body with multiple exit points #2273

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}