From 24008cdffb65457db5e14b296693ec619886ee20 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 4 Jun 2018 18:37:59 +0300 Subject: [PATCH] If to when: do not add label to outer loop if not necessary So #KT-24767 Fixed --- .../intentions/IfToWhenIntention.kt | 5 ++++- .../intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt | 5 +++++ .../branched/ifWhen/ifToWhen/withLoopNoJumps.kt.after | 7 +++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 5 +++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt create mode 100644 idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt index 4709052ce96..97420c00778 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt @@ -89,11 +89,14 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres } } + var labelRequired = false + fun KtExpressionWithLabel.addLabelIfNecessary(): KtExpressionWithLabel { if (this.getLabelName() != null) return this if (this.getStrictParentOfType() != nearestLoopIfAny) return this if (labelName != null) { val jumpWithLabel = KtPsiFactory(project).createExpression("$text@$labelName") as KtExpressionWithLabel + labelRequired = true return replaced(jumpWithLabel) } return this @@ -187,7 +190,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres result.accept(loopJumpVisitor) val labelName = loopJumpVisitor.labelName - if (loop != null && labelName != null && loop.parent !is KtLabeledExpression) { + if (loop != null && loopJumpVisitor.labelRequired && labelName != null && loop.parent !is KtLabeledExpression) { val labeledLoopExpression = KtPsiFactory(result).createLabeledExpression(labelName) labeledLoopExpression.baseExpression!!.replace(loop) val replacedLabeledLoopExpression = loop.replace(labeledLoopExpression) diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt new file mode 100644 index 00000000000..39eda52ef56 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt @@ -0,0 +1,5 @@ +fun test() { + for (i in -2..2) { + if (i > 0) i.hashCode() + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt.after new file mode 100644 index 00000000000..087b7a2b8de --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt.after @@ -0,0 +1,7 @@ +fun test() { + for (i in -2..2) { + when { + i > 0 -> i.hashCode() + } + } +} \ 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 128cedeb74f..1fcce7afba6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2619,6 +2619,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopExistingLabel.kt"); } + @TestMetadata("withLoopNoJumps.kt") + public void testWithLoopNoJumps() throws Exception { + runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopNoJumps.kt"); + } + @TestMetadata("withLoopOriginal.kt") public void testWithLoopOriginal() throws Exception { runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopOriginal.kt");