FIR checker: Enable ReplaceWithDotCallFix for UNNECESSARY_SAFE_CALL.

This commit is contained in:
Mark Punzalan
2021-04-14 00:03:29 +00:00
committed by Ilya Kirillov
parent f1eeb72c01
commit 957f6ddafd
9 changed files with 57 additions and 16 deletions
@@ -1136,6 +1136,7 @@ fun main(args: Array<String>) {
model("quickfix/lateinit", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/modifiers", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/override/typeMismatchOnOverride", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/replaceWithDotCall", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/replaceWithSafeCall", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/variables/changeMutability", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/addInitializer", pattern = pattern, filenameStartsLowerCase = true)
@@ -81,6 +81,7 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
}
private val expressions = KtQuickFixesListBuilder.registerPsiQuickFix {
registerPsiQuickFixes(KtFirDiagnostic.UnnecessarySafeCall::class, ReplaceWithDotCallFix)
registerApplicator(ReplaceCallFixFactories.unsafeCallFactory)
}
@@ -344,6 +344,11 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
runTest("idea/testData/quickfix/expressions/unnecessarySafeCall1.kt");
}
@TestMetadata("unnecessarySafeCall2.kt")
public void testUnnecessarySafeCall2() throws Exception {
runTest("idea/testData/quickfix/expressions/unnecessarySafeCall2.kt");
}
@TestMetadata("unsafeCall1.kt")
public void testUnsafeCall1() throws Exception {
runTest("idea/testData/quickfix/expressions/unsafeCall1.kt");
@@ -874,6 +879,39 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
}
}
@TestMetadata("idea/testData/quickfix/replaceWithDotCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceWithDotCall extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInReplaceWithDotCall() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/replaceWithDotCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("comment.kt")
public void testComment() throws Exception {
runTest("idea/testData/quickfix/replaceWithDotCall/comment.kt");
}
@TestMetadata("functionCall.kt")
public void testFunctionCall() throws Exception {
runTest("idea/testData/quickfix/replaceWithDotCall/functionCall.kt");
}
@TestMetadata("lineBreak.kt")
public void testLineBreak() throws Exception {
runTest("idea/testData/quickfix/replaceWithDotCall/lineBreak.kt");
}
@TestMetadata("normal.kt")
public void testNormal() throws Exception {
runTest("idea/testData/quickfix/replaceWithDotCall/normal.kt");
}
}
@TestMetadata("idea/testData/quickfix/replaceWithSafeCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -11,7 +11,6 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -92,10 +91,10 @@ class ReplaceWithSafeCallForScopeFunctionFix(
class ReplaceWithDotCallFix(expression: KtSafeQualifiedExpression) : ReplaceCallFix(expression, "."), CleanupFix {
override fun getText() = KotlinBundle.message("replace.with.dot.call")
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val qualifiedExpression = diagnostic.psiElement.getParentOfType<KtSafeQualifiedExpression>(strict = false) ?: return null
return ReplaceWithDotCallFix(qualifiedExpression)
companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
val qualifiedExpression = psiElement.getParentOfType<KtSafeQualifiedExpression>(strict = false) ?: return emptyList()
return listOfNotNull(ReplaceWithDotCallFix(qualifiedExpression))
}
}
}
+1 -3
View File
@@ -1,6 +1,4 @@
// "Replace with dot call" "true"
fun foo(a: Any) {
a<caret>?.equals(0)
}
/* IGNORE_FIR */
}
@@ -1,6 +1,4 @@
// "Replace with dot call" "true"
fun foo(a: Any) {
a<caret>.equals(0)
}
/* IGNORE_FIR */
}
@@ -0,0 +1,4 @@
// "Replace with dot call" "true"
fun Any.foo() {
this<caret>?.equals(0)
}
@@ -1,7 +1,4 @@
// "Replace with dot call" "true"
fun foo(a: Any) {
when (a) {
.equals(0) => true
else => false
}
fun Any.foo() {
this.equals(0)
}
@@ -7757,6 +7757,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/expressions/unnecessarySafeCall1.kt");
}
@TestMetadata("unnecessarySafeCall2.kt")
public void testUnnecessarySafeCall2() throws Exception {
runTest("idea/testData/quickfix/expressions/unnecessarySafeCall2.kt");
}
@TestMetadata("unsafeCall1.kt")
public void testUnsafeCall1() throws Exception {
runTest("idea/testData/quickfix/expressions/unsafeCall1.kt");