From 8ba5548a0f4c74b987098fba75f21f3cc4053599 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 19 Aug 2020 18:24:58 +0900 Subject: [PATCH] "Eliminate argument of 'when'": do not suggest if 'when' is used as expression and it has no 'else' branch (#2898) #KT-35526 Fixed --- .../intentions/EliminateWhenSubjectIntention.kt | 8 +++++++- .../branched/when/eliminateSubject/withoutElse.kt | 11 +++++++++++ .../when/eliminateSubject/withoutElse.kt.after | 11 +++++++++++ .../eliminateSubject/withoutElseUsedAsExpression.kt | 12 ++++++++++++ .../idea/intentions/IntentionTestGenerated.java | 10 ++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt create mode 100644 idea/testData/intentions/branched/when/eliminateSubject/withoutElse.kt.after create mode 100644 idea/testData/intentions/branched/when/eliminateSubject/withoutElseUsedAsExpression.kt 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")