If then to elvis: no more allowed for if expressions with Unit result
This commit is contained in:
+5
-3
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
|
|||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import org.jetbrains.kotlin.KtNodeTypes
|
import org.jetbrains.kotlin.KtNodeTypes
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
@@ -29,7 +30,6 @@ import org.jetbrains.kotlin.lexer.KtTokens
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode.Companion.PARTIAL
|
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.typeUtil.TypeNullability
|
import org.jetbrains.kotlin.types.typeUtil.TypeNullability
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||||
@@ -54,12 +54,14 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
override fun isApplicableTo(element: KtIfExpression): Boolean {
|
||||||
|
val context = element.analyze()
|
||||||
|
val type = element.getType(context) ?: return false
|
||||||
|
if (KotlinBuiltIns.isUnit(type)) return false
|
||||||
|
|
||||||
val condition = element.condition as? KtOperationExpression ?: return false
|
val condition = element.condition as? KtOperationExpression ?: return false
|
||||||
val thenClause = element.then ?: return false
|
val thenClause = element.then ?: return false
|
||||||
val elseClause = element.`else` ?: return false
|
val elseClause = element.`else` ?: return false
|
||||||
|
|
||||||
val context = condition.analyze(PARTIAL)
|
|
||||||
|
|
||||||
val checkedExpression = condition.checkedExpression() ?: return false
|
val checkedExpression = condition.checkedExpression() ?: return false
|
||||||
if (!checkedExpression.isStableVariable(context)) return false
|
if (!checkedExpression.isStableVariable(context)) return false
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
open class Some {
|
||||||
|
fun bar() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
object Obj : Some()
|
||||||
|
|
||||||
|
fun foo(arg: Any?) {
|
||||||
|
<caret>if (arg is Some) {
|
||||||
|
arg.bar()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Obj.bar()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1452,6 +1452,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("unitResult.kt")
|
||||||
|
public void testUnitResult() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/unitResult.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("willNotInlineClassProperty.kt")
|
@TestMetadata("willNotInlineClassProperty.kt")
|
||||||
public void testWillNotInlineClassProperty() throws Exception {
|
public void testWillNotInlineClassProperty() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user