diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt index 0501169cf06..22b2c0e97ec 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/EliminateWhenSubjectIntention.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.SelfTargetingIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.toExpression import org.jetbrains.kotlin.idea.util.CommentSaver @@ -16,6 +17,8 @@ import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.KtWhenExpression import org.jetbrains.kotlin.psi.buildExpression import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class EliminateWhenSubjectIntention : SelfTargetingIntention(KtWhenExpression::class.java, KotlinBundle.lazyMessage("eliminate.argument.of.when")), @@ -23,7 +26,10 @@ class EliminateWhenSubjectIntention : override fun isApplicableTo(element: KtWhenExpression, caretOffset: Int): Boolean { if (element.subjectExpression !is KtNameReferenceExpression) return false val lBrace = element.openBrace ?: return false - return caretOffset <= lBrace.startOffset + if (caretOffset > lBrace.startOffset) return false + val lastEntry = element.entries.lastOrNull() + if (lastEntry?.isElse != true && element.isUsedAsExpression(element.analyze(BodyResolveMode.PARTIAL_WITH_CFA))) return false + return true } override fun applyTo(element: KtWhenExpression, editor: Editor?) { diff --git a/idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt b/idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt new file mode 100644 index 00000000000..8a643fb74ad --- /dev/null +++ b/idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt @@ -0,0 +1,11 @@ +enum class Type { + HYDRO, + PYRO +} + +fun select(t: Type) { + when (t) { + Type.HYDRO -> 1 + Type.PYRO -> 42 + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt.after b/idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt.after new file mode 100644 index 00000000000..eafabbfdb89 --- /dev/null +++ b/idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt.after @@ -0,0 +1,11 @@ +enum class Type { + HYDRO, + PYRO +} + +fun select(t: Type) { + when { + t == Type.HYDRO -> 1 + t == Type.PYRO -> 42 + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/when/eliminateSubject/withoutElseUsedAsExpression.kt b/idea/testData/intentions/branched/when/eliminateSubject/withoutElseUsedAsExpression.kt new file mode 100644 index 00000000000..1d4cf5a7e9a --- /dev/null +++ b/idea/testData/intentions/branched/when/eliminateSubject/withoutElseUsedAsExpression.kt @@ -0,0 +1,12 @@ +// IS_APPLICABLE: false +enum class Type { + HYDRO, + PYRO +} + +fun select(t: Type): Int { + return when (t) { + Type.HYDRO -> 1 + Type.PYRO -> 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 8f32c5e64d1..41ef4523197 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -3267,6 +3267,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { public void testWhenWithoutSubject() throws Exception { runTest("idea/testData/intentions/branched/when/eliminateSubject/whenWithoutSubject.kt"); } + + @TestMetadata("withoutElse.kt") + public void testWithoutElse() throws Exception { + runTest("idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt"); + } + + @TestMetadata("withoutElseUsedAsExpression.kt") + public void testWithoutElseUsedAsExpression() throws Exception { + runTest("idea/testData/intentions/branched/when/eliminateSubject/withoutElseUsedAsExpression.kt"); + } } @TestMetadata("idea/testData/intentions/branched/when/flatten")