FIR IDE: Enable RemoveUselessCastFix.

This commit is contained in:
Mark Punzalan
2021-05-27 18:07:31 +00:00
committed by teamcityserver
parent f5a53c82c5
commit 639b7537da
28 changed files with 35 additions and 71 deletions
@@ -106,6 +106,7 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
registerPsiQuickFixes(KtFirDiagnostic.UnnecessaryNotNullAssertion::class, RemoveExclExclCallFix) registerPsiQuickFixes(KtFirDiagnostic.UnnecessaryNotNullAssertion::class, RemoveExclExclCallFix)
registerPsiQuickFixes(KtFirDiagnostic.UselessElvis::class, RemoveUselessElvisFix) registerPsiQuickFixes(KtFirDiagnostic.UselessElvis::class, RemoveUselessElvisFix)
registerPsiQuickFixes(KtFirDiagnostic.UselessElvisRightIsNull::class, RemoveUselessElvisFix) registerPsiQuickFixes(KtFirDiagnostic.UselessElvisRightIsNull::class, RemoveUselessElvisFix)
registerPsiQuickFixes(KtFirDiagnostic.UselessCast::class, RemoveUselessCastFix)
registerApplicator(ReplaceCallFixFactories.unsafeCallFactory) registerApplicator(ReplaceCallFixFactories.unsafeCallFactory)
registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory) registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory)
registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory) registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory)
@@ -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. * 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 package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project 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.KotlinBundle
import org.jetbrains.kotlin.idea.core.dropEnclosingParenthesesIfPossible import org.jetbrains.kotlin.idea.core.dropEnclosingParenthesesIfPossible
import org.jetbrains.kotlin.idea.core.replaced 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.KtFile
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>(element), class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinPsiOnlyQuickFixAction<KtBinaryExpressionWithTypeRHS>(element),
CleanupFix { CleanupFix {
override fun getFamilyName() = KotlinBundle.message("remove.useless.cast") override fun getFamilyName() = KotlinBundle.message("remove.useless.cast")
@@ -25,12 +26,12 @@ class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinQuick
invoke(element ?: return) invoke(element ?: return)
} }
companion object : KotlinSingleIntentionActionFactory() { companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
operator fun invoke(element: KtBinaryExpressionWithTypeRHS) = element.replaced(element.left).dropEnclosingParenthesesIfPossible() operator fun invoke(element: KtBinaryExpressionWithTypeRHS) = element.replaced(element.left).dropEnclosingParenthesesIfPossible()
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? { override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null val expression = psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return emptyList()
return RemoveUselessCastFix(expression) return listOf(RemoveUselessCastFix(expression))
} }
} }
} }
+1 -3
View File
@@ -1,6 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun foo(a: String) { fun foo(a: String) {
val b = a <caret>as String val b = a <caret>as String
} }
/* IGNORE_FIR */
@@ -1,6 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun foo(a: String) { fun foo(a: String) {
val b = a val b = a
} }
/* IGNORE_FIR */
@@ -5,5 +5,4 @@ fun test() {
foo() foo()
// comment // comment
((({ "" } as<caret> () -> String))) ((({ "" } as<caret> () -> String)))
} }
/* IGNORE_FIR */
@@ -5,5 +5,4 @@ fun test() {
foo() foo()
// comment // comment
({ "" }) ({ "" })
} }
/* IGNORE_FIR */
@@ -1,5 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun test() { fun test() {
((({ "" } as<caret> () -> String))) ((({ "" } as<caret> () -> String)))
} }
/* IGNORE_FIR */
@@ -1,5 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun test() { fun test() {
{ "" } { "" }
} }
/* IGNORE_FIR */
@@ -1,5 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun test() { fun test() {
({ "" } as<caret> () -> String) ({ "" } as<caret> () -> String)
} }
/* IGNORE_FIR */
@@ -1,5 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun test() { fun test() {
{ "" } { "" }
} }
/* IGNORE_FIR */
@@ -5,5 +5,4 @@ fun test() {
foo() foo()
// comment // comment
({ "" } as<caret> () -> String) ({ "" } as<caret> () -> String)
} }
/* IGNORE_FIR */
@@ -5,5 +5,4 @@ fun test() {
foo() foo()
// comment // comment
({ "" }) ({ "" })
} }
/* IGNORE_FIR */
@@ -6,5 +6,4 @@ class A {
fun test() { fun test() {
A().foo() A().foo()
({ "" } as<caret> () -> String) ({ "" } as<caret> () -> String)
} }
/* IGNORE_FIR */
@@ -6,5 +6,4 @@ class A {
fun test() { fun test() {
A().foo() A().foo()
({ "" }) ({ "" })
} }
/* IGNORE_FIR */
@@ -2,5 +2,4 @@
fun test() { fun test() {
class A() class A()
({ "" } as<caret> () -> String) ({ "" } as<caret> () -> String)
} }
/* IGNORE_FIR */
@@ -2,5 +2,4 @@
fun test() { fun test() {
class A() class A()
({ "" }) ({ "" })
} }
/* IGNORE_FIR */
@@ -4,5 +4,4 @@ open class A
fun test() { fun test() {
class B : A() class B : A()
({ "" } as<caret> () -> String) ({ "" } as<caret> () -> String)
} }
/* IGNORE_FIR */
@@ -4,5 +4,4 @@ open class A
fun test() { fun test() {
class B : A() class B : A()
({ "" }) ({ "" })
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun foo() {}
fun main() { fun main() {
foo(); foo();
({ "" } as<caret> () -> String) ({ "" } as<caret> () -> String)
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun foo() {}
fun main() { fun main() {
foo(); foo();
{ "" } { "" }
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return (((x <caret>as String))).length return (((x <caret>as String))).length
} }
return -1 return -1
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return x.length return x.length
} }
return -1 return -1
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return (x <caret>as String).length return (x <caret>as String).length
} }
return -1 return -1
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return x.length return x.length
} }
return -1 return -1
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun test(x: Any): String? {
return x <caret>as String return x <caret>as String
} }
return null return null
} }
/* IGNORE_FIR */
@@ -4,6 +4,4 @@ fun test(x: Any): String? {
return x return x
} }
return null return null
} }
/* IGNORE_FIR */
@@ -1,6 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun foo(a: Any) { fun foo(a: Any) {
val b = a <caret>as Any val b = a <caret>as Any
} }
/* IGNORE_FIR */
@@ -1,6 +1,4 @@
// "Remove useless cast" "true" // "Remove useless cast" "true"
fun foo(a: Any) { fun foo(a: Any) {
val b = a<caret> val b = a<caret>
} }
/* IGNORE_FIR */