diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/MainKtQuickFixRegistrar.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/MainKtQuickFixRegistrar.kt index e31367c241e..758b7dbdb0e 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/MainKtQuickFixRegistrar.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/quickfix/MainKtQuickFixRegistrar.kt @@ -106,6 +106,7 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() { registerPsiQuickFixes(KtFirDiagnostic.UnnecessaryNotNullAssertion::class, RemoveExclExclCallFix) registerPsiQuickFixes(KtFirDiagnostic.UselessElvis::class, RemoveUselessElvisFix) registerPsiQuickFixes(KtFirDiagnostic.UselessElvisRightIsNull::class, RemoveUselessElvisFix) + registerPsiQuickFixes(KtFirDiagnostic.UselessCast::class, RemoveUselessCastFix) registerApplicator(ReplaceCallFixFactories.unsafeCallFactory) registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory) registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessCastFix.kt b/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessCastFix.kt similarity index 63% rename from idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessCastFix.kt rename to idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessCastFix.kt index 4a2d70836e8..fe750b608d5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessCastFix.kt +++ b/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveUselessCastFix.kt @@ -1,13 +1,14 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.idea.quickfix +import com.intellij.codeInsight.intention.IntentionAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import org.jetbrains.kotlin.diagnostics.Diagnostic +import com.intellij.psi.PsiElement import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.core.dropEnclosingParenthesesIfPossible import org.jetbrains.kotlin.idea.core.replaced @@ -15,7 +16,7 @@ import org.jetbrains.kotlin.psi.KtBinaryExpressionWithTypeRHS import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType -class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinQuickFixAction(element), +class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinPsiOnlyQuickFixAction(element), CleanupFix { override fun getFamilyName() = KotlinBundle.message("remove.useless.cast") @@ -25,12 +26,12 @@ class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinQuick invoke(element ?: return) } - companion object : KotlinSingleIntentionActionFactory() { + companion object : QuickFixesPsiBasedFactory(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) { operator fun invoke(element: KtBinaryExpressionWithTypeRHS) = element.replaced(element.left).dropEnclosingParenthesesIfPossible() - override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { - val expression = diagnostic.psiElement.getNonStrictParentOfType() ?: return null - return RemoveUselessCastFix(expression) + override fun doCreateQuickFix(psiElement: PsiElement): List { + val expression = psiElement.getNonStrictParentOfType() ?: return emptyList() + return listOf(RemoveUselessCastFix(expression)) } } } diff --git a/idea/testData/quickfix/expressions/removeUselessCast.kt b/idea/testData/quickfix/expressions/removeUselessCast.kt index f10844780f7..8cc147045e7 100644 --- a/idea/testData/quickfix/expressions/removeUselessCast.kt +++ b/idea/testData/quickfix/expressions/removeUselessCast.kt @@ -1,6 +1,4 @@ // "Remove useless cast" "true" fun foo(a: String) { val b = a as String -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCast.kt.after b/idea/testData/quickfix/expressions/removeUselessCast.kt.after index 184ffbdf8ca..d0c32639126 100644 --- a/idea/testData/quickfix/expressions/removeUselessCast.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCast.kt.after @@ -1,6 +1,4 @@ // "Remove useless cast" "true" fun foo(a: String) { val b = a -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt index e9cc904f8b8..cf1d55276ca 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt @@ -5,5 +5,4 @@ fun test() { foo() // comment ((({ "" } as () -> String))) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt.after index fb1192c5a1e..370e99c7de7 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNecessaryNestedParens2.kt.after @@ -5,5 +5,4 @@ fun test() { foo() // comment ({ "" }) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt index 0e16f44bb1d..2ad82368124 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt @@ -1,5 +1,4 @@ // "Remove useless cast" "true" fun test() { ((({ "" } as () -> String))) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt.after index 9939faaaaec..e4038d4b644 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInNestedParens.kt.after @@ -1,5 +1,4 @@ // "Remove useless cast" "true" fun test() { { "" } -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt index 3a4b3792c0f..6a59aee2ffc 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt @@ -1,5 +1,4 @@ // "Remove useless cast" "true" fun test() { ({ "" } as () -> String) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt.after index 9939faaaaec..e4038d4b644 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens1.kt.after @@ -1,5 +1,4 @@ // "Remove useless cast" "true" fun test() { { "" } -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt index 0651244a19a..a9135199793 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt @@ -5,5 +5,4 @@ fun test() { foo() // comment ({ "" } as () -> String) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt.after index fb1192c5a1e..370e99c7de7 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens2.kt.after @@ -5,5 +5,4 @@ fun test() { foo() // comment ({ "" }) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt index 61ca1bba9ce..ea4f8e4ccab 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt @@ -6,5 +6,4 @@ class A { fun test() { A().foo() ({ "" } as () -> String) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt.after index c64a42e6e91..e03f360d5a0 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens3.kt.after @@ -6,5 +6,4 @@ class A { fun test() { A().foo() ({ "" }) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt index 887b82e0daa..e00d9d50169 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt @@ -2,5 +2,4 @@ fun test() { class A() ({ "" } as () -> String) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt.after index 361e168543a..f9949426d4d 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens4.kt.after @@ -2,5 +2,4 @@ fun test() { class A() ({ "" }) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt index 8c11a1cddc0..1edb7b01f75 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt @@ -4,5 +4,4 @@ open class A fun test() { class B : A() ({ "" } as () -> String) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt.after index 30f966573e3..53ec2a04545 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens5.kt.after @@ -4,5 +4,4 @@ open class A fun test() { class B : A() ({ "" }) -} -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt index 40cc6194182..c7d896405cc 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt @@ -4,6 +4,4 @@ fun foo() {} fun main() { foo(); ({ "" } as () -> String) -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt.after b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt.after index 754a714fa06..44e41119b31 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastForLambdaInParens6.kt.after @@ -4,6 +4,4 @@ fun foo() {} fun main() { foo(); { "" } -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt b/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt index 460d34ffd5a..ddc9cc503bc 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt @@ -4,6 +4,4 @@ fun test(x: Any): Int { return (((x as String))).length } return -1 -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt.after b/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt.after index 3c49e654ca4..a0f44b32ce4 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastInNestedParens.kt.after @@ -4,6 +4,4 @@ fun test(x: Any): Int { return x.length } return -1 -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastInParens.kt b/idea/testData/quickfix/expressions/removeUselessCastInParens.kt index 5af1dc32e9b..d535bfade1f 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastInParens.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastInParens.kt @@ -4,6 +4,4 @@ fun test(x: Any): Int { return (x as String).length } return -1 -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastInParens.kt.after b/idea/testData/quickfix/expressions/removeUselessCastInParens.kt.after index 3c49e654ca4..a0f44b32ce4 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastInParens.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastInParens.kt.after @@ -4,6 +4,4 @@ fun test(x: Any): Int { return x.length } return -1 -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt b/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt index d93cc82d1a9..241546de1f1 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt +++ b/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt @@ -4,6 +4,4 @@ fun test(x: Any): String? { return x as String } return null -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt.after b/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt.after index 0955d43b1c6..90b65d21d91 100644 --- a/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt.after +++ b/idea/testData/quickfix/expressions/removeUselessCastUnderSmartCast.kt.after @@ -4,6 +4,4 @@ fun test(x: Any): String? { return x } return null -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt b/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt index 5c9a46ea261..58a353220cf 100644 --- a/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt +++ b/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt @@ -1,6 +1,4 @@ // "Remove useless cast" "true" fun foo(a: Any) { val b = a as Any -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file diff --git a/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt.after b/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt.after index ccd6a10e2c8..799d00fb4de 100644 --- a/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt.after +++ b/idea/testData/quickfix/expressions/uselessCastStaticAssertIsFine.kt.after @@ -1,6 +1,4 @@ // "Remove useless cast" "true" fun foo(a: Any) { val b = a -} - -/* IGNORE_FIR */ \ No newline at end of file +} \ No newline at end of file