Fold initializer & if to elvis: add test, rename tests, simplify code
Enhancement for KT-27016
This commit is contained in:
+3
-5
@@ -23,7 +23,9 @@ import com.intellij.psi.PsiElement
|
|||||||
import com.intellij.psi.PsiWhiteSpace
|
import com.intellij.psi.PsiWhiteSpace
|
||||||
import com.intellij.psi.search.LocalSearchScope
|
import com.intellij.psi.search.LocalSearchScope
|
||||||
import com.intellij.psi.search.searches.ReferencesSearch
|
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.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.core.setType
|
import org.jetbrains.kotlin.idea.core.setType
|
||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
@@ -147,11 +149,7 @@ class FoldInitializerAndIfToElvisIntention :
|
|||||||
|
|
||||||
if (typeReference != null) {
|
if (typeReference != null) {
|
||||||
val checkedType = ifExpression.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference]
|
val checkedType = ifExpression.analyze(BodyResolveMode.PARTIAL)[BindingContext.TYPE, typeReference]
|
||||||
val variableTypeReference = prevStatement.typeReference
|
val variableType = (prevStatement.resolveToDescriptorIfAny() as? VariableDescriptor)?.type
|
||||||
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
|
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()
|
||||||
|
}
|
||||||
+20
-15
@@ -8729,6 +8729,26 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/ifStatementPriority.kt");
|
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")
|
@TestMetadata("MultiStatementBlock.kt")
|
||||||
public void testMultiStatementBlock() throws Exception {
|
public void testMultiStatementBlock() throws Exception {
|
||||||
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/MultiStatementBlock.kt");
|
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/MultiStatementBlock.kt");
|
||||||
@@ -8749,21 +8769,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt");
|
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")
|
@TestMetadata("OtherVar1.kt")
|
||||||
public void testOtherVar1() throws Exception {
|
public void testOtherVar1() throws Exception {
|
||||||
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/OtherVar1.kt");
|
runTest("idea/testData/intentions/foldInitializerAndIfToElvis/OtherVar1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user