FIR IDE: RemoveNulalbleFix quickfix

This commit is contained in:
Tianyu Geng
2021-06-11 16:30:40 -07:00
committed by Ilya Kirillov
parent b10de3dd2d
commit 4bedf41f9c
10 changed files with 99 additions and 59 deletions
@@ -1171,7 +1171,7 @@ fun main(args: Array<String>) {
model("quickfix/expressions", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/lateinit", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/modifiers", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/nullables/unsafeInfixCall", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/nullables", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/override", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/override/typeMismatchOnOverride", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/replaceInfixOrOperatorCall", pattern = pattern, filenameStartsLowerCase = true)
@@ -132,6 +132,9 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
registerApplicator(WrapWithSafeLetCallFixFactories.forUnsafeInfixCall)
registerApplicator(WrapWithSafeLetCallFixFactories.forUnsafeOperatorCall)
registerApplicator(WrapWithSafeLetCallFixFactories.forArgumentTypeMismatch)
registerPsiQuickFixes(KtFirDiagnostic.NullableSupertype::class, RemoveNullableFix.removeForSuperType)
registerPsiQuickFixes(KtFirDiagnostic.InapplicableLateinitModifier::class, RemoveNullableFix.removeForLateInitProperty)
}
private val returnTypes = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -1133,76 +1133,109 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
}
}
@TestMetadata("idea/testData/quickfix/nullables/unsafeInfixCall")
@TestMetadata("idea/testData/quickfix/nullables")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class UnsafeInfixCall extends AbstractHighLevelQuickFixTest {
public static class Nullables extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInUnsafeInfixCall() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/nullables/unsafeInfixCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
public void testAllFilesPresentInNullables() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/nullables"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("noComparison.kt")
public void testNoComparison() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/noComparison.kt");
@TestMetadata("removeRedundantNullable.kt")
public void testRemoveRedundantNullable() throws Exception {
runTest("idea/testData/quickfix/nullables/removeRedundantNullable.kt");
}
@TestMetadata("unsafeComparisonInCondition.kt")
public void testUnsafeComparisonInCondition() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInCondition.kt");
@TestMetadata("removeSupertypeNullable1.kt")
public void testRemoveSupertypeNullable1() throws Exception {
runTest("idea/testData/quickfix/nullables/removeSupertypeNullable1.kt");
}
@TestMetadata("unsafeComparisonInLogic.kt")
public void testUnsafeComparisonInLogic() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInLogic.kt");
@TestMetadata("removeSupertypeNullable2.kt")
public void testRemoveSupertypeNullable2() throws Exception {
runTest("idea/testData/quickfix/nullables/removeSupertypeNullable2.kt");
}
@TestMetadata("unsafeComparisonInWhen.kt")
public void testUnsafeComparisonInWhen() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInWhen.kt");
@TestMetadata("removeUselessNullable.kt")
public void testRemoveUselessNullable() throws Exception {
runTest("idea/testData/quickfix/nullables/removeUselessNullable.kt");
}
@TestMetadata("unsafeComparisonInWhile.kt")
public void testUnsafeComparisonInWhile() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInWhile.kt");
}
@TestMetadata("idea/testData/quickfix/nullables/unsafeInfixCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class UnsafeInfixCall extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("unsafeGet.kt")
public void testUnsafeGet() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeGet.kt");
}
public void testAllFilesPresentInUnsafeInfixCall() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/nullables/unsafeInfixCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("unsafeInfixCall.kt")
public void testUnsafeInfixCall() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInfixCall.kt");
}
@TestMetadata("noComparison.kt")
public void testNoComparison() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/noComparison.kt");
}
@TestMetadata("unsafeInvoke.kt")
public void testUnsafeInvoke() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvoke.kt");
}
@TestMetadata("unsafeComparisonInCondition.kt")
public void testUnsafeComparisonInCondition() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInCondition.kt");
}
@TestMetadata("unsafeInvokeWithImplicitReceiver.kt")
public void testUnsafeInvokeWithImplicitReceiver() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvokeWithImplicitReceiver.kt");
}
@TestMetadata("unsafeComparisonInLogic.kt")
public void testUnsafeComparisonInLogic() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInLogic.kt");
}
@TestMetadata("unsafeInvokeWithReceiver.kt")
public void testUnsafeInvokeWithReceiver() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvokeWithReceiver.kt");
}
@TestMetadata("unsafeComparisonInWhen.kt")
public void testUnsafeComparisonInWhen() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInWhen.kt");
}
@TestMetadata("unsafePlus.kt")
public void testUnsafePlus() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafePlus.kt");
}
@TestMetadata("unsafeComparisonInWhile.kt")
public void testUnsafeComparisonInWhile() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeComparisonInWhile.kt");
}
@TestMetadata("unsafeSet.kt")
public void testUnsafeSet() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeSet.kt");
@TestMetadata("unsafeGet.kt")
public void testUnsafeGet() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeGet.kt");
}
@TestMetadata("unsafeInfixCall.kt")
public void testUnsafeInfixCall() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInfixCall.kt");
}
@TestMetadata("unsafeInvoke.kt")
public void testUnsafeInvoke() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvoke.kt");
}
@TestMetadata("unsafeInvokeWithImplicitReceiver.kt")
public void testUnsafeInvokeWithImplicitReceiver() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvokeWithImplicitReceiver.kt");
}
@TestMetadata("unsafeInvokeWithReceiver.kt")
public void testUnsafeInvokeWithReceiver() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvokeWithReceiver.kt");
}
@TestMetadata("unsafePlus.kt")
public void testUnsafePlus() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafePlus.kt");
}
@TestMetadata("unsafeSet.kt")
public void testUnsafeSet() throws Exception {
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeSet.kt");
}
}
}
@@ -8,10 +8,7 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveNullableFix(element: KtNullableType, private val typeOfError: NullableKind) :
@@ -43,7 +40,10 @@ class RemoveNullableFix(element: KtNullableType, private val typeOfError: Nullab
return quickFixesPsiBasedFactory { e ->
when (typeOfError) {
NullableKind.REDUNDANT, NullableKind.SUPERTYPE, NullableKind.USELESS -> {
val nullType = e.getNonStrictParentOfType<KtNullableType>()
val nullType: KtNullableType? = when (e) {
is KtTypeReference -> e.typeElement as? KtNullableType
else -> e.getNonStrictParentOfType()
}
if (nullType?.innerType == null) return@quickFixesPsiBasedFactory emptyList()
listOf(RemoveNullableFix(nullType, typeOfError))
}
+1 -2
View File
@@ -2,5 +2,4 @@
class A() {
<caret>lateinit var foo: String?
}
/* IGNORE_FIR */
}
+1 -2
View File
@@ -2,5 +2,4 @@
class A() {
<caret>lateinit var foo: String
}
/* IGNORE_FIR */
}
@@ -2,3 +2,5 @@
fun main(args : Array<String>) {
val x : Int??<caret> = 15
}
/* IGNORE_FIR */
@@ -2,3 +2,5 @@
fun main(args : Array<String>) {
val x : Int? = 15
}
/* IGNORE_FIR */
+2 -1
View File
@@ -1,4 +1,5 @@
// "Remove useless '?'" "true"
fun f(a: Int) : Boolean {
return a is Int?<caret>
}
}
/* IGNORE_FIR */
@@ -1,4 +1,5 @@
// "Remove useless '?'" "true"
fun f(a: Int) : Boolean {
return a is Int<caret>
}
}
/* IGNORE_FIR */