"Create enum constant" quick fix: suggest if expected type is Any

#KT-22666 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-12-14 22:41:58 +09:00
committed by klunnii
parent 31ed5430ee
commit 14bdcb1e26
4 changed files with 24 additions and 0 deletions
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.Qualifier
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.substitutions.getTypeSubstitution
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
import org.jetbrains.kotlin.types.typeUtil.isUnit
import java.util.*
import org.jetbrains.kotlin.descriptors.ClassKind as ClassDescriptorKind
@@ -98,6 +99,10 @@ internal fun KotlinType.toClassTypeInfo(): TypeInfo {
}
internal fun getClassKindFilter(expectedType: KotlinType, containingDeclaration: PsiElement): (ClassKind) -> Boolean {
if (expectedType.isAnyOrNullableAny()) {
return { _ -> true }
}
val descriptor = expectedType.constructor.declarationDescriptor ?: return { _ -> false }
val canHaveSubtypes = !(expectedType.constructor.isFinal || expectedType.containsStarProjections()) || expectedType.isUnit()
@@ -0,0 +1,7 @@
// "Create enum constant 'RED'" "true"
enum class SampleEnum {}
fun usage(sample: SampleEnum) {
if (sample == SampleEnum.RED<caret>) {
}
}
@@ -0,0 +1,7 @@
// "Create enum constant 'RED'" "true"
enum class SampleEnum { RED }
fun usage(sample: SampleEnum) {
if (sample == SampleEnum.RED) {
}
}
@@ -3764,6 +3764,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumEntryWithSuperclass.kt");
}
@TestMetadata("enumInBinaryExpression.kt")
public void testEnumInBinaryExpression() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumInBinaryExpression.kt");
}
@TestMetadata("enumNoReceiver.kt")
public void testEnumNoReceiver() throws Exception {
runTest("idea/testData/quickfix/createFromUsage/createClass/referenceExpression/enumNoReceiver.kt");