If then to elvis: now not applicable for is with type unrelated to original #KT-14520 Fixed
This commit is contained in:
+5
-1
@@ -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.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.nullability
|
import org.jetbrains.kotlin.types.typeUtil.nullability
|
||||||
|
|
||||||
class IfThenToElvisInspection : IntentionBasedInspection<KtIfExpression>(IfThenToElvisIntention::class)
|
class IfThenToElvisInspection : IntentionBasedInspection<KtIfExpression>(IfThenToElvisIntention::class)
|
||||||
@@ -69,7 +70,10 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
is KtIsExpression -> {
|
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) {
|
when (condition.isNegated) {
|
||||||
true -> checkedExpression.clausesReplaceableByElvis(thenClause, elseClause, context)
|
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);
|
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")
|
@TestMetadata("isCheckWithSelector.kt")
|
||||||
public void testIsCheckWithSelector() throws Exception {
|
public void testIsCheckWithSelector() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelector.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/isCheckWithSelector.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user