"Eliminate argument of 'when'": do not suggest if 'when' is used as expression and it has no 'else' branch (#2898)

#KT-35526 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-08-19 18:24:58 +09:00
committed by GitHub
parent d965ad0a98
commit 8ba5548a0f
5 changed files with 51 additions and 1 deletions
@@ -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>(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?) {
@@ -0,0 +1,11 @@
enum class Type {
HYDRO,
PYRO
}
fun select(t: Type) {
<caret>when (t) {
Type.HYDRO -> 1
Type.PYRO -> 42
}
}
@@ -0,0 +1,11 @@
enum class Type {
HYDRO,
PYRO
}
fun select(t: Type) {
when {
t == Type.HYDRO -> 1
t == Type.PYRO -> 42
}
}
@@ -0,0 +1,12 @@
// IS_APPLICABLE: false
enum class Type {
HYDRO,
PYRO
}
fun select(t: Type): Int {
return <caret>when (t) {
Type.HYDRO -> 1
Type.PYRO -> 42
}
}
@@ -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")