From 49e5f170d1da02f3c27cef72d136ff182fb39634 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 19 Dec 2019 09:22:47 +0900 Subject: [PATCH] "Replace 'when' with 'if'" intention: do not suggest if 'when' subject is variable declaration #KT-35528 Fixed --- .../intentions/WhenToIfIntention.kt | 6 ++---- .../branched/ifWhen/whenToIf/variableSubject.kt | 13 +++++++++++++ .../idea/intentions/IntentionTestGenerated.java | 5 +++++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 idea/testData/intentions/branched/ifWhen/whenToIf/variableSubject.kt 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 82119e6f5b6..28b46514b1e 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 @@ -11,10 +11,7 @@ import com.intellij.openapi.util.TextRange 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.KtIfExpression -import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.KtWhenExpression -import org.jetbrains.kotlin.psi.buildExpression +import org.jetbrains.kotlin.psi.* class WhenToIfIntention : SelfTargetingRangeIntention(KtWhenExpression::class.java, "Replace 'when' with 'if'"), LowPriorityAction { @@ -24,6 +21,7 @@ class WhenToIfIntention : SelfTargetingRangeIntention(KtWhenEx val lastEntry = entries.last() 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 return element.whenKeyword.textRange } diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/variableSubject.kt b/idea/testData/intentions/branched/ifWhen/whenToIf/variableSubject.kt new file mode 100644 index 00000000000..6f5ea58d181 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/variableSubject.kt @@ -0,0 +1,13 @@ +// IS_APPLICABLE: false +enum class Type { + HYDRO, + PYRO +} + +fun select(t: Type): Int { + return when (val i = t.ordinal) { + 0 -> 1 + 1 -> 42 + else -> i + } +} \ 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 aa2d0c5c06e..8f0c1f27616 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2525,6 +2525,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifWhen/whenToIf/kt13884.kt"); } + @TestMetadata("variableSubject.kt") + public void testVariableSubject() throws Exception { + runTest("idea/testData/intentions/branched/ifWhen/whenToIf/variableSubject.kt"); + } + @TestMetadata("whenWithDotQualifiedExpression.kt") public void testWhenWithDotQualifiedExpression() throws Exception { runTest("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt");