[kotlin] k2 extract function: include return without value in jump expressions

- ensure non-conditional call is separated from the previous statement

^KTIJ-29320 fixed

GitOrigin-RevId: d7d9a03cfcf991e311a8b9f15e03840a6a189575
This commit is contained in:
Anna Kozlova
2024-04-22 07:53:17 +00:00
committed by intellij-monorepo-bot
parent 634c54e7e6
commit 06081e8246
12 changed files with 72 additions and 14 deletions
@@ -1411,6 +1411,11 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/extractFunction/controlFlow/exitPointEquivalence/breakAndReturn.kt");
}
@TestMetadata("breakAndReturn1.kt")
public void testBreakAndReturn1() throws Exception {
runTest("testData/refactoring/extractFunction/controlFlow/exitPointEquivalence/breakAndReturn1.kt");
}
@TestMetadata("breakContinueAndDefault.kt")
public void testBreakContinueAndDefault() throws Exception {
runTest("testData/refactoring/extractFunction/controlFlow/exitPointEquivalence/breakContinueAndDefault.kt");
@@ -5,6 +5,4 @@ fun main() {
<selection>// comment
if (true) return
println()</selection>
}
// IGNORE_K1
}
@@ -1,7 +1,7 @@
fun main() {
println()
__dummyTestFun__()
}
private fun __dummyTestFun__() {
@@ -9,5 +9,3 @@ private fun __dummyTestFun__() {
if (true) return
println()
}
// IGNORE_K1
@@ -0,0 +1,19 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
__dummyTestFun__(a, b)
return
}
}
private fun __dummyTestFun__(a: Int, b: Int) {
if (a > 0) throw Exception("")
if (a + b > 0) return
println(a - b)
return
}
@@ -0,0 +1,15 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) break
println(a - b)
return</selection>
}
println()
}
@@ -0,0 +1 @@
Selected code fragment has multiple exit points
@@ -10,6 +10,4 @@ fun foo(a: Int) {
if (b + a > 0) return
println(a - b)</selection>
println(a + b)
}
// IGNORE_K2
}
@@ -15,6 +15,4 @@ private fun b(a: Int, b: Int): Boolean {
if (b + a > 0) return true
println(a - b)
return false
}
// IGNORE_K2
}
@@ -0,0 +1,18 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
if (bool(a, b)) return
println(a + b)
}
private fun bool(a: Int, b: Int): Boolean {
if (a > 0) throw Exception("")
if (b + a > 0) return true
println(a - b)
return false
}
@@ -404,7 +404,10 @@ abstract class ExtractFunctionGenerator<KotlinType, ExtractionResult : IExtracti
is Jump -> {
val elementToInsertAfterCall = outputValue.elementToInsertAfterCall
when {
elementToInsertAfterCall == null -> Collections.singletonList(psiFactory.createExpression(callText))
elementToInsertAfterCall == null -> listOf(
newLine,
psiFactory.createExpression(callText)
)
outputValue.conditional -> Collections.singletonList(
psiFactory.createExpression("if ($callText) ${elementToInsertAfterCall.text}")
)
@@ -175,7 +175,7 @@ internal class ExtractionDataAnalyzer(private val extractionData: ExtractionData
typeOfDefaultFlow = approximateWithResolvableType(typeOfDefaultFlow, scope) ?: builtinTypes.UNIT,
valuedReturnExpressions = exitSnapshot.valuedReturnExpressions.filter { it is KtReturnExpression },
returnValueType = approximateWithResolvableType(exitSnapshot.returnValueType, scope) ?: builtinTypes.UNIT,
jumpExpressions = exitSnapshot.jumpExpressions.filter { it is KtBreakExpression || it is KtContinueExpression },
jumpExpressions = exitSnapshot.jumpExpressions.filter { it is KtBreakExpression || it is KtContinueExpression || it is KtReturnExpression && it.returnedExpression == null},
hasSingleTarget = !exitSnapshot.hasMultipleJumpTargets,
sameExitForDefaultAndJump = if (exitSnapshot.hasJumps) !exitSnapshot.hasEscapingJumps && defaultExpressionInfo != null else defaultExpressionInfo == null
)
@@ -579,6 +579,11 @@ public abstract class K2IntroduceFunctionTestGenerated extends AbstractK2Introdu
runTest("../../idea/tests/testData/refactoring/extractFunction/controlFlow/exitPointEquivalence/breakAndReturn.kt");
}
@TestMetadata("breakAndReturn1.kt")
public void testBreakAndReturn1() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunction/controlFlow/exitPointEquivalence/breakAndReturn1.kt");
}
@TestMetadata("breakContinueAndDefault.kt")
public void testBreakContinueAndDefault() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunction/controlFlow/exitPointEquivalence/breakContinueAndDefault.kt");