FIR IDE: Enable RemoveUselessIsCheckFix and

RemoveUselessIsCheckFixForWhen.
This commit is contained in:
Mark Punzalan
2021-05-27 20:16:10 +00:00
committed by teamcityserver
parent 639b7537da
commit af35892007
16 changed files with 59 additions and 98 deletions
@@ -107,6 +107,7 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
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) registerPsiQuickFixes(KtFirDiagnostic.UselessCast::class, RemoveUselessCastFix)
registerPsiQuickFixes(KtFirDiagnostic.UselessIsCheck::class, RemoveUselessIsCheckFix, RemoveUselessIsCheckFixForWhen)
registerApplicator(ReplaceCallFixFactories.unsafeCallFactory) registerApplicator(ReplaceCallFixFactories.unsafeCallFactory)
registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory) registerApplicator(AddExclExclCallFixFactories.unsafeCallFactory)
registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory) registerApplicator(AddExclExclCallFixFactories.unsafeInfixCallFactory)
@@ -0,0 +1,37 @@
/*
* 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 com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtIsExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveUselessIsCheckFix(element: KtIsExpression) : KotlinPsiOnlyQuickFixAction<KtIsExpression>(element) {
override fun getFamilyName() = KotlinBundle.message("remove.useless.is.check")
override fun getText(): String = familyName
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.run {
val expressionsText = this.isNegated.not().toString()
val newExpression = KtPsiFactory(project).createExpression(expressionsText)
replace(newExpression)
}
}
companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
val expression = psiElement.getNonStrictParentOfType<KtIsExpression>() ?: return emptyList()
return listOf(RemoveUselessIsCheckFix(expression))
}
}
}
@@ -1,29 +1,19 @@
/* /*
* Copyright 2010-2017 JetBrains s.r.o. * 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.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
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.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveUselessIsCheckFixForWhen(element: KtWhenConditionIsPattern) : KotlinQuickFixAction<KtWhenConditionIsPattern>(element) { class RemoveUselessIsCheckFixForWhen(element: KtWhenConditionIsPattern) : KotlinPsiOnlyQuickFixAction<KtWhenConditionIsPattern>(element) {
override fun getFamilyName() = KotlinBundle.message("remove.useless.is.check") override fun getFamilyName() = KotlinBundle.message("remove.useless.is.check")
override fun getText(): String = familyName override fun getText(): String = familyName
@@ -43,10 +33,10 @@ class RemoveUselessIsCheckFixForWhen(element: KtWhenConditionIsPattern) : Kotlin
} }
} }
companion object : KotlinSingleIntentionActionFactory() { companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtWhenConditionIsPattern>? { override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtWhenConditionIsPattern>() ?: return null val expression = psiElement.getNonStrictParentOfType<KtWhenConditionIsPattern>() ?: return emptyList()
return RemoveUselessIsCheckFixForWhen(expression) return listOf(RemoveUselessIsCheckFixForWhen(expression))
} }
} }
} }
@@ -1,47 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtIsExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveUselessIsCheckFix(element: KtIsExpression) : KotlinQuickFixAction<KtIsExpression>(element) {
override fun getFamilyName() = KotlinBundle.message("remove.useless.is.check")
override fun getText(): String = familyName
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.run {
val expressionsText = this.isNegated.not().toString()
val newExpression = KtPsiFactory(project).createExpression(expressionsText)
replace(newExpression)
}
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtIsExpression>? {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtIsExpression>() ?: return null
return RemoveUselessIsCheckFix(expression)
}
}
}
@@ -4,5 +4,3 @@ fun foo(a: String) {
} }
} }
/* IGNORE_FIR */
@@ -4,5 +4,3 @@ fun foo(a: String) {
} }
} }
/* IGNORE_FIR */
@@ -9,5 +9,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -8,5 +8,3 @@ fun foo(bar: Base):Int {
else -> 42 else -> 42
} }
} }
/* IGNORE_FIR */
@@ -10,4 +10,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -10,4 +10,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -10,4 +10,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -10,4 +10,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -9,5 +9,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -8,5 +8,3 @@ fun foo(bar: Base):Int {
else -> 1 else -> 1
} }
} }
/* IGNORE_FIR */
@@ -4,5 +4,3 @@ fun foo(a: String) {
} }
} }
/* IGNORE_FIR */
@@ -4,5 +4,3 @@ fun foo(a: String) {
} }
} }
/* IGNORE_FIR */