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 97420c00778..f42646e7692 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 @@ -63,7 +63,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres builder.append(nextSibling.text) nextSibling = nextSibling.nextSibling ?: break } - KtPsiFactory(ifExpression).createBlock(builder.toString()) + KtPsiFactory(ifExpression).createBlock(builder.toString()).takeIf { it.statements.isNotEmpty() } } } } diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt b/idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt new file mode 100644 index 00000000000..4c7705fb3e2 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt @@ -0,0 +1,8 @@ +fun test(a: Any, b: Boolean): Int { + when (a) { + is Int -> { + if (b) return 1 + } + } + return 0 +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt.after new file mode 100644 index 00000000000..cea16764a33 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt.after @@ -0,0 +1,10 @@ +fun test(a: Any, b: Boolean): Int { + when (a) { + is Int -> { + when { + b -> return 1 + } + } + } + return 0 +} \ 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 df8d4c84dc3..771fa187d68 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2514,6 +2514,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt"); } + @TestMetadata("ifThenReturn.kt") + public void testIfThenReturn() throws Exception { + runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt"); + } + @TestMetadata("ifWithEqualityTests.kt") public void testIfWithEqualityTests() throws Exception { runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithEqualityTests.kt");