"Replace 'if' with elvis operator": don't suggest on super type check

#KT-27016 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-09-26 13:18:45 +03:00
committed by Mikhail Glukhikh
parent ffa3d7c57a
commit fd6f34f8ca
7 changed files with 95 additions and 0 deletions
@@ -35,9 +35,11 @@ import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
@@ -143,6 +145,16 @@ class FoldInitializerAndIfToElvisIntention :
val then = ifExpression.then ?: return null
val typeReference = (operationExpression as? KtIsExpression)?.typeReference
if (typeReference != null) {
val checkedType = ifExpression.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference]
val variableTypeReference = prevStatement.typeReference
val variableType = if (variableTypeReference != null)
prevStatement.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, variableTypeReference]
else
SpecifyTypeExplicitlyIntention.getTypeForDeclaration(prevStatement)
if (checkedType != null && variableType != null && !checkedType.isSubtypeOf(variableType)) return null
}
val statement = if (then is KtBlockExpression) then.statements.singleOrNull() else then
statement ?: return null
@@ -0,0 +1,13 @@
open class A
open class B : A() {
fun b() {}
}
fun test() {
val b = B()
if (<caret>b !is B) {
return
}
b.b()
}
@@ -0,0 +1,10 @@
open class A
open class B : A() {
fun b() {}
}
fun test() {
val b = B() as? B ?: return
b.b()
}
@@ -0,0 +1,17 @@
open class A
open class B : A() {
fun b() {}
}
open class C : B() {
fun c() {}
}
fun test() {
val b = B()
if (<caret>b !is C) {
return
}
b.b()
}
@@ -0,0 +1,14 @@
open class A
open class B : A() {
fun b() {}
}
open class C : B() {
fun c() {}
}
fun test() {
val b = B() as? C ?: return
b.b()
}
@@ -0,0 +1,14 @@
// IS_APPLICABLE: false
open class A
open class B : A() {
fun b() {}
}
fun test() {
val b = B()
if (<caret>b !is A) {
return
}
b.b()
}
@@ -8749,6 +8749,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt");
}
@TestMetadata("NotIsSameType.kt")
public void testNotIsSameType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsSameType.kt");
}
@TestMetadata("NotIsSubType.kt")
public void testNotIsSubType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsSubType.kt");
}
@TestMetadata("NotIsSuperType.kt")
public void testNotIsSuperType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsSuperType.kt");
}
@TestMetadata("OtherVar1.kt")
public void testOtherVar1() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/OtherVar1.kt");