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.UselessElvis::class, RemoveUselessElvisFix)
registerPsiQuickFixes(KtFirDiagnostic.UselessElvisRightIsNull::class, RemoveUselessElvisFix)
registerPsiQuickFixes(KtFirDiagnostic.UselessCast::class, RemoveUselessCastFix)
registerApplicator(ReplaceCallFixFactories.unsafeCallFactory)
registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory)
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.
*/
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<KtBinaryExpressionWithTypeRHS>(element),
class RemoveUselessCastFix(element: KtBinaryExpressionWithTypeRHS) : KotlinPsiOnlyQuickFixAction<KtBinaryExpressionWithTypeRHS>(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>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
operator fun invoke(element: KtBinaryExpressionWithTypeRHS) = element.replaced(element.left).dropEnclosingParenthesesIfPossible()
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null
return RemoveUselessCastFix(expression)
override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
val expression = psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return emptyList()
return listOf(RemoveUselessCastFix(expression))
}
}
}
+1 -3
View File
@@ -1,6 +1,4 @@
// "Remove useless cast" "true"
fun foo(a: String) {
val b = a <caret>as String
}
/* IGNORE_FIR */
}
@@ -1,6 +1,4 @@
// "Remove useless cast" "true"
fun foo(a: String) {
val b = a
}
/* IGNORE_FIR */
}
@@ -5,5 +5,4 @@ fun test() {
foo()
// comment
((({ "" } as<caret> () -> String)))
}
/* IGNORE_FIR */
}
@@ -5,5 +5,4 @@ fun test() {
foo()
// comment
({ "" })
}
/* IGNORE_FIR */
}
@@ -1,5 +1,4 @@
// "Remove useless cast" "true"
fun test() {
((({ "" } as<caret> () -> String)))
}
/* IGNORE_FIR */
}
@@ -1,5 +1,4 @@
// "Remove useless cast" "true"
fun test() {
{ "" }
}
/* IGNORE_FIR */
}
@@ -1,5 +1,4 @@
// "Remove useless cast" "true"
fun test() {
({ "" } as<caret> () -> String)
}
/* IGNORE_FIR */
}
@@ -1,5 +1,4 @@
// "Remove useless cast" "true"
fun test() {
{ "" }
}
/* IGNORE_FIR */
}
@@ -5,5 +5,4 @@ fun test() {
foo()
// comment
({ "" } as<caret> () -> String)
}
/* IGNORE_FIR */
}
@@ -5,5 +5,4 @@ fun test() {
foo()
// comment
({ "" })
}
/* IGNORE_FIR */
}
@@ -6,5 +6,4 @@ class A {
fun test() {
A().foo()
({ "" } as<caret> () -> String)
}
/* IGNORE_FIR */
}
@@ -6,5 +6,4 @@ class A {
fun test() {
A().foo()
({ "" })
}
/* IGNORE_FIR */
}
@@ -2,5 +2,4 @@
fun test() {
class A()
({ "" } as<caret> () -> String)
}
/* IGNORE_FIR */
}
@@ -2,5 +2,4 @@
fun test() {
class A()
({ "" })
}
/* IGNORE_FIR */
}
@@ -4,5 +4,4 @@ open class A
fun test() {
class B : A()
({ "" } as<caret> () -> String)
}
/* IGNORE_FIR */
}
@@ -4,5 +4,4 @@ open class A
fun test() {
class B : A()
({ "" })
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun foo() {}
fun main() {
foo();
({ "" } as<caret> () -> String)
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun foo() {}
fun main() {
foo();
{ "" }
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return (((x <caret>as String))).length
}
return -1
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return x.length
}
return -1
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return (x <caret>as String).length
}
return -1
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun test(x: Any): Int {
return x.length
}
return -1
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun test(x: Any): String? {
return x <caret>as String
}
return null
}
/* IGNORE_FIR */
}
@@ -4,6 +4,4 @@ fun test(x: Any): String? {
return x
}
return null
}
/* IGNORE_FIR */
}
@@ -1,6 +1,4 @@
// "Remove useless cast" "true"
fun foo(a: Any) {
val b = a <caret>as Any
}
/* IGNORE_FIR */
}
@@ -1,6 +1,4 @@
// "Remove useless cast" "true"
fun foo(a: Any) {
val b = a<caret>
}
/* IGNORE_FIR */
}