提交 f3c25c72 编写于 作者: M Mikhail Glukhikh

If then to elvis: no more allowed for if expressions with `Unit` result

上级 8edd33ee
......@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.replaced
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.resolve.BindingContext
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.typeUtil.TypeNullability
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
......@@ -54,12 +54,14 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
}
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 thenClause = element.then ?: return false
val elseClause = element.`else` ?: return false
val context = condition.analyze(PARTIAL)
val checkedExpression = condition.checkedExpression() ?: return false
if (!checkedExpression.isStableVariable(context)) return false
......
// 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()
}
}
\ No newline at end of file
......@@ -1452,6 +1452,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
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")
public void testWillNotInlineClassProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/willNotInlineClassProperty.kt");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册