diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt index fe5f8799377..f917761d0e6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt @@ -9,10 +9,13 @@ import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.combineWhenConditions import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class WhenToIfIntention : SelfTargetingRangeIntention( KtWhenExpression::class.java, @@ -25,6 +28,7 @@ class WhenToIfIntention : SelfTargetingRangeIntention( if (entries.any { it != lastEntry && it.isElse }) return null if (entries.all { it.isElse }) return null // 'when' with only 'else' branch is not supported if (element.subjectExpression is KtProperty) return null + if (!lastEntry.isElse && element.isUsedAsExpression(element.analyze(BodyResolveMode.PARTIAL_WITH_CFA))) return null return element.whenKeyword.textRange } diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutElseUsedAsExpression.kt b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutElseUsedAsExpression.kt new file mode 100644 index 00000000000..bea81545b74 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutElseUsedAsExpression.kt @@ -0,0 +1,10 @@ +//IS_APPLICABLE: false +enum class E { X, Y, Z} + +fun test(e: E) { + val i = when (e) { + E.X -> 1 + E.Y -> 2 + E.Z -> 3 + } +} \ 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 b421e904d64..41732dcbe9f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2653,6 +2653,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutElse.kt"); } + @TestMetadata("whenWithoutElseUsedAsExpression.kt") + public void testWhenWithoutElseUsedAsExpression() throws Exception { + runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutElseUsedAsExpression.kt"); + } + @TestMetadata("whenWithoutSubject.kt") public void testWhenWithoutSubject() throws Exception { runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutSubject.kt");