From 39016594b109cc2a003c70e47f950e4e984550c0 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sat, 16 Mar 2019 21:41:10 +0900 Subject: [PATCH] Replace return with 'if'/'when': don't drop return label #KT-30414 Fixed --- .../intentions/UnfoldReturnToIfIntention.kt | 15 +++++++++++---- .../intentions/UnfoldReturnToWhenIntention.kt | 3 ++- .../unfolding/returnToIf/labeledReturn.kt | 10 ++++++++++ .../unfolding/returnToIf/labeledReturn.kt.after | 10 ++++++++++ .../unfolding/returnToWhen/labeledReturn.kt | 10 ++++++++++ .../unfolding/returnToWhen/labeledReturn.kt.after | 10 ++++++++++ .../idea/intentions/IntentionTestGenerated.java | 10 ++++++++++ 7 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt create mode 100644 idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt.after create mode 100644 idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt create mode 100644 idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToIfIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToIfIntention.kt index 07850c28bb5..f9af332c3a4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToIfIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToIfIntention.kt @@ -50,16 +50,23 @@ class UnfoldReturnToIfIntention : LowPriorityAction, SelfTargetingRangeIntention val psiFactory = KtPsiFactory(element) val context = element.analyze() - newThenExpr.replace(createReturnExpression(thenExpr, psiFactory, context)) - newElseExpr.replace(createReturnExpression(elseExpr, psiFactory, context)) + val labelName = element.getLabelName() + newThenExpr.replace(createReturnExpression(thenExpr, labelName, psiFactory, context)) + newElseExpr.replace(createReturnExpression(elseExpr, labelName, psiFactory, context)) element.replace(newIfExpression) } 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) { 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) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToWhenIntention.kt index 0afb9ca3ef7..451aa8f7a04 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/UnfoldReturnToWhenIntention.kt @@ -44,10 +44,11 @@ class UnfoldReturnToWhenIntention : LowPriorityAction, SelfTargetingRangeIntenti val whenExpression = element.returnedExpression as KtWhenExpression val newWhenExpression = whenExpression.copied() + val labelName = element.getLabelName() whenExpression.entries.zip(newWhenExpression.entries).forEach { (entry, newEntry) -> val expr = entry.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) } diff --git a/idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt b/idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt new file mode 100644 index 00000000000..1bbd9e81924 --- /dev/null +++ b/idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun main() { + run label@{ + return@label if (true) { + 42 + } else { + 42 + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt.after b/idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt.after new file mode 100644 index 00000000000..0b1d8c458fc --- /dev/null +++ b/idea/testData/intentions/branched/unfolding/returnToIf/labeledReturn.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun main() { + run label@{ + if (true) { + return@label 42 + } else { + return@label 42 + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt b/idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt new file mode 100644 index 00000000000..01d251b0fb8 --- /dev/null +++ b/idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun main() { + run label@{ + return@label when { + true -> + 42 + else -> 42 + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt.after b/idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt.after new file mode 100644 index 00000000000..f6b5adf6a7a --- /dev/null +++ b/idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt.after @@ -0,0 +1,10 @@ +// WITH_RUNTIME +fun main() { + run label@{ + when { + true -> + return@label 42 + else -> return@label 42 + } + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index d6ef10acca6..1a7122a60f8 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -3416,6 +3416,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { 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") public void testSimpleIf() throws Exception { 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"); } + @TestMetadata("labeledReturn.kt") + public void testLabeledReturn() throws Exception { + runTest("idea/testData/intentions/branched/unfolding/returnToWhen/labeledReturn.kt"); + } + @TestMetadata("simpleWhen.kt") public void testSimpleWhen() throws Exception { runTest("idea/testData/intentions/branched/unfolding/returnToWhen/simpleWhen.kt");