Skip to content

Commit e2a6cd2

Browse files
Do not replace function body with multiple exit points (#2273)
* Do not replace function body having a return statement only in case the return statement contains an intermediate exit point Closes #2269
1 parent b135fe8 commit e2a6cd2

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1414
* Fix indent of multiline object declaration inside class `indent` [#2257](https://github.com/pinterest/ktlint/issue/2257)
1515
* Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260)
1616
* 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)
17+
* 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)
1718

1819
### Changed
1920

ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionExpressionBodyRule.kt

+6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
2121
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
2222
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
2323
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY
24+
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
2425
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
26+
import com.pinterest.ktlint.rule.engine.core.api.leavesIncludingSelf
2527
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
2628
import com.pinterest.ktlint.rule.engine.core.api.prevSibling
2729
import com.pinterest.ktlint.ruleset.standard.StandardRule
@@ -109,6 +111,7 @@ public class FunctionExpressionBodyRule :
109111
require(block.elementType == BLOCK)
110112
block
111113
.takeIf { it.containingOnly(RETURN) }
114+
?.takeUnless { it.containsMultipleReturns() }
112115
?.findChildByType(RETURN)
113116
?.findChildByType(RETURN_KEYWORD)
114117
?.nextSibling { !it.isWhiteSpace() }
@@ -161,6 +164,9 @@ public class FunctionExpressionBodyRule :
161164
.singleOrNull()
162165
?.elementType
163166

167+
private fun ASTNode.containsMultipleReturns() =
168+
firstChildLeafOrSelf().leavesIncludingSelf().count { it.elementType == RETURN_KEYWORD } > 1
169+
164170
private fun ASTNode.createUnitTypeReference() =
165171
PsiFileFactory
166172
.getInstance(psi.project)

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionExpressionBodyRuleTest.kt

+15
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,19 @@ class FunctionExpressionBodyRuleTest {
153153
.isFormattedAs(formattedCode)
154154
.hasLintViolation(1, 16, "Function body should be replaced with body expression")
155155
}
156+
157+
@Test
158+
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`() {
159+
val code =
160+
"""
161+
fun foo(): Any {
162+
return if (true) {
163+
Foo()
164+
} else {
165+
return Bar()
166+
}
167+
}
168+
""".trimIndent()
169+
functionExpressionBodyRule(code).hasNoLintViolations()
170+
}
156171
}

0 commit comments

Comments
 (0)