KT-21929 Inappropriate quick fix for a sealed class instantiation (#1444)
This commit is contained in:
committed by
Dmitry Jemerov
parent
de185b79d0
commit
16695c1af5
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
|
||||
@@ -25,6 +26,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory3
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
|
||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
||||
@@ -42,6 +44,8 @@ object MakeVisibleFactory : KotlinIntentionActionsFactory() {
|
||||
val descriptor = factory.cast(diagnostic).c as? DeclarationDescriptorWithVisibility ?: return emptyList()
|
||||
val declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) as? KtModifierListOwner ?: return emptyList()
|
||||
|
||||
if (declaration.hasModifier(KtTokens.SEALED_KEYWORD) && descriptor is ClassConstructorDescriptor) return emptyList()
|
||||
|
||||
val module = DescriptorUtils.getContainingModule(descriptor)
|
||||
val targetVisibilities = when (descriptor.visibility) {
|
||||
PRIVATE, INVISIBLE_FAKE -> mutableListOf(PUBLIC).apply {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Make '<init>' public" "false"
|
||||
// "Make '<init>' internal" "false"
|
||||
// ERROR: Cannot access '<init>': it is private in 'SealedClass'
|
||||
// ERROR: This type is sealed, so it can be inherited by only its own nested classes or objects
|
||||
|
||||
sealed class SealedClass
|
||||
|
||||
fun test() {
|
||||
class Test : <caret>SealedClass()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Make 'SealedClass' public" "true"
|
||||
// ACTION: "Make 'Test' private"
|
||||
|
||||
private sealed class SealedClass
|
||||
|
||||
class Test : <caret>SealedClass()
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Make 'SealedClass' public" "true"
|
||||
// ACTION: "Make 'Test' private"
|
||||
|
||||
sealed class SealedClass
|
||||
|
||||
class Test : SealedClass()
|
||||
@@ -6683,6 +6683,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("invalidSealedClassInheritance.kt")
|
||||
public void testInvalidSealedClassInheritance() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/increaseVisibility/invalidSealedClassInheritance.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("overriddenProtectedMemberToPublicSingleFile.kt")
|
||||
public void testOverriddenProtectedMemberToPublicSingleFile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/increaseVisibility/overriddenProtectedMemberToPublicSingleFile.kt");
|
||||
@@ -6707,6 +6713,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("privateSealedClassInheritance.kt")
|
||||
public void testPrivateSealedClassInheritance() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/increaseVisibility/privateSealedClassInheritance.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("protectedMemberToPublicSingleFile.kt")
|
||||
public void testProtectedMemberToPublicSingleFile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/increaseVisibility/protectedMemberToPublicSingleFile.kt");
|
||||
|
||||
Reference in New Issue
Block a user