If then to elvis: now not applicable for is with type unrelated to original #KT-14520 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-10-27 14:56:08 +03:00
parent 87d265dc1b
commit e7cef79709
3 changed files with 23 additions and 1 deletions
@@ -32,6 +32,7 @@ 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
import org.jetbrains.kotlin.types.typeUtil.nullability
class IfThenToElvisInspection : IntentionBasedInspection<KtIfExpression>(IfThenToElvisIntention::class)
@@ -69,7 +70,10 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
else -> false
}
is KtIsExpression -> {
if (!context[BindingContext.TYPE, condition.typeReference].isNotNull()) return false
val targetType = context[BindingContext.TYPE, condition.typeReference] ?: return false
if (!targetType.isNotNull()) return false
val originalType = condition.leftHandSide.getType(context) ?: return false
if (!targetType.isSubtypeOf(originalType)) return false
when (condition.isNegated) {
true -> checkedExpression.clausesReplaceableByElvis(thenClause, elseClause, context)
@@ -0,0 +1,12 @@
// IS_APPLICABLE: false
interface A
interface B
fun prepare(x: A) = x
fun use(x: A) {}
fun test(x: A) {
val xx = <caret>if (x is B) x else prepare(x)
use(xx)
}
@@ -1308,6 +1308,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("isCheckForUnrelatedType.kt")
public void testIsCheckForUnrelatedType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckForUnrelatedType.kt");
doTest(fileName);
}
@TestMetadata("isCheckWithSelector.kt")
public void testIsCheckWithSelector() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelector.kt");