Replace return with 'if'/'when': don't drop return label
#KT-30414 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
9a2178d96b
commit
39016594b1
+11
-4
@@ -50,16 +50,23 @@ class UnfoldReturnToIfIntention : LowPriorityAction, SelfTargetingRangeIntention
|
|||||||
val psiFactory = KtPsiFactory(element)
|
val psiFactory = KtPsiFactory(element)
|
||||||
val context = element.analyze()
|
val context = element.analyze()
|
||||||
|
|
||||||
newThenExpr.replace(createReturnExpression(thenExpr, psiFactory, context))
|
val labelName = element.getLabelName()
|
||||||
newElseExpr.replace(createReturnExpression(elseExpr, psiFactory, context))
|
newThenExpr.replace(createReturnExpression(thenExpr, labelName, psiFactory, context))
|
||||||
|
newElseExpr.replace(createReturnExpression(elseExpr, labelName, psiFactory, context))
|
||||||
element.replace(newIfExpression)
|
element.replace(newIfExpression)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun createReturnExpression(expr: KtExpression, psiFactory: KtPsiFactory, context: BindingContext): KtExpression {
|
fun createReturnExpression(
|
||||||
|
expr: KtExpression,
|
||||||
|
labelName: String?,
|
||||||
|
psiFactory: KtPsiFactory,
|
||||||
|
context: BindingContext
|
||||||
|
): KtExpression {
|
||||||
|
val label = labelName?.let { "@$it" } ?: ""
|
||||||
val returnText = when (expr) {
|
val returnText = when (expr) {
|
||||||
is KtBreakExpression, is KtContinueExpression, is KtReturnExpression, is KtThrowExpression -> ""
|
is KtBreakExpression, is KtContinueExpression, is KtReturnExpression, is KtThrowExpression -> ""
|
||||||
else -> if (expr.getResolvedCall(context)?.resultingDescriptor?.returnType?.isNothing() == true) "" else "return "
|
else -> if (expr.getResolvedCall(context)?.resultingDescriptor?.returnType?.isNothing() == true) "" else "return$label "
|
||||||
}
|
}
|
||||||
return psiFactory.createExpressionByPattern("$returnText$0", expr)
|
return psiFactory.createExpressionByPattern("$returnText$0", expr)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -44,10 +44,11 @@ class UnfoldReturnToWhenIntention : LowPriorityAction, SelfTargetingRangeIntenti
|
|||||||
val whenExpression = element.returnedExpression as KtWhenExpression
|
val whenExpression = element.returnedExpression as KtWhenExpression
|
||||||
val newWhenExpression = whenExpression.copied()
|
val newWhenExpression = whenExpression.copied()
|
||||||
|
|
||||||
|
val labelName = element.getLabelName()
|
||||||
whenExpression.entries.zip(newWhenExpression.entries).forEach { (entry, newEntry) ->
|
whenExpression.entries.zip(newWhenExpression.entries).forEach { (entry, newEntry) ->
|
||||||
val expr = entry.expression!!.lastBlockStatementOrThis()
|
val expr = entry.expression!!.lastBlockStatementOrThis()
|
||||||
val newExpr = newEntry.expression!!.lastBlockStatementOrThis()
|
val newExpr = newEntry.expression!!.lastBlockStatementOrThis()
|
||||||
newExpr.replace(UnfoldReturnToIfIntention.createReturnExpression(expr, psiFactory, context))
|
newExpr.replace(UnfoldReturnToIfIntention.createReturnExpression(expr, labelName, psiFactory, context))
|
||||||
}
|
}
|
||||||
element.replace(newWhenExpression)
|
element.replace(newWhenExpression)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun main() {
|
||||||
|
run label@{
|
||||||
|
<caret>return@label if (true) {
|
||||||
|
42
|
||||||
|
} else {
|
||||||
|
42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun main() {
|
||||||
|
run label@{
|
||||||
|
<caret>if (true) {
|
||||||
|
return@label 42
|
||||||
|
} else {
|
||||||
|
return@label 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun main() {
|
||||||
|
run label@{
|
||||||
|
<caret>return@label when {
|
||||||
|
true ->
|
||||||
|
42
|
||||||
|
else -> 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun main() {
|
||||||
|
run label@{
|
||||||
|
<caret>when {
|
||||||
|
true ->
|
||||||
|
return@label 42
|
||||||
|
else -> return@label 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3416,6 +3416,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/branched/unfolding/returnToIf/innerIfTransformed.kt");
|
runTest("idea/testData/intentions/branched/unfolding/returnToIf/innerIfTransformed.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("labeledReturn.kt")
|
||||||
|
public void testLabeledReturn() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleIf.kt")
|
@TestMetadata("simpleIf.kt")
|
||||||
public void testSimpleIf() throws Exception {
|
public void testSimpleIf() throws Exception {
|
||||||
runTest("idea/testData/intentions/branched/unfolding/returnToIf/simpleIf.kt");
|
runTest("idea/testData/intentions/branched/unfolding/returnToIf/simpleIf.kt");
|
||||||
@@ -3444,6 +3449,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/branched/unfolding/returnToWhen/innerWhenTransformed.kt");
|
runTest("idea/testData/intentions/branched/unfolding/returnToWhen/innerWhenTransformed.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("labeledReturn.kt")
|
||||||
|
public void testLabeledReturn() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleWhen.kt")
|
@TestMetadata("simpleWhen.kt")
|
||||||
public void testSimpleWhen() throws Exception {
|
public void testSimpleWhen() throws Exception {
|
||||||
runTest("idea/testData/intentions/branched/unfolding/returnToWhen/simpleWhen.kt");
|
runTest("idea/testData/intentions/branched/unfolding/returnToWhen/simpleWhen.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user