Fold initializer & if to elvis: add test, rename tests, simplify code

Enhancement for KT-27016
This commit is contained in:
Mikhail Glukhikh
2018-11-29 13:10:50 +03:00
parent fd6f34f8ca
commit 9b4bcabbd3
8 changed files with 44 additions and 20 deletions
@@ -23,7 +23,9 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.search.LocalSearchScope
import com.intellij.psi.search.searches.ReferencesSearch
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.core.setType
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
@@ -147,11 +149,7 @@ class FoldInitializerAndIfToElvisIntention :
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)
val variableType = (prevStatement.resolveToDescriptorIfAny() as? VariableDescriptor)?.type
if (checkedType != null && variableType != null && !checkedType.isSubtypeOf(variableType)) return null
}
@@ -0,0 +1,21 @@
// IS_APPLICABLE: false
interface A {
fun a() {}
}
open class B : A {
fun b() {}
}
interface C : A {
fun c() {}
}
fun test() {
val b = B()
if (<caret>b !is C) {
return
}
b.b()
b.c()
}
@@ -8729,6 +8729,26 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/ifStatementPriority.kt");
}
@TestMetadata("IsSameType.kt")
public void testIsSameType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSameType.kt");
}
@TestMetadata("IsSideTypeFake.kt")
public void testIsSideTypeFake() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSideTypeFake.kt");
}
@TestMetadata("IsSubType.kt")
public void testIsSubType() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSubType.kt");
}
@TestMetadata("IsSuperTypeFake.kt")
public void testIsSuperTypeFake() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/IsSuperTypeFake.kt");
}
@TestMetadata("MultiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception {
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/MultiStatementBlock.kt");
@@ -8749,21 +8769,6 @@ 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");