Add quickfix for SENSELESS_NULL_IN_WHEN to remove redundant when branch

^KT-24556 Fixed

Original commit: 5abb6bc7a54ed7b3536687f4448a073bc6a4b58d

GitOrigin-RevId: 072637aa0dd4c1f0d9f678da48fd73ffb997594f
This commit is contained in:
Toshiaki Kameyama
2020-10-01 14:33:06 +03:00
committed by intellij-monorepo-bot
parent a0fccb234d
commit 3aecbad9dd
6 changed files with 42 additions and 5 deletions
@@ -1154,6 +1154,7 @@ remove.useless.is.check=Remove useless is check
remove.val.var.from.parameter=Remove 'val/var' from parameter
remove.0.from.parameter=Remove ''{0}'' from parameter
remove.else.branch=Remove else branch
remove.branch=Remove branch
rename.identifier.fix.text=Rename
rename.to.0=Rename to ''{0}''
rename.parameter.to.match.overridden.method=Rename parameter to match overridden method
@@ -276,7 +276,8 @@ class QuickFixRegistrar : QuickFixContributor {
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix)
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix)
NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix)
REDUNDANT_ELSE_IN_WHEN.registerFactory(RemoveWhenElseBranchFix)
REDUNDANT_ELSE_IN_WHEN.registerFactory(RemoveWhenBranchFix)
SENSELESS_NULL_IN_WHEN.registerFactory(RemoveWhenBranchFix)
NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenElseBranchFix)
NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenRemainingBranchesFix)
NON_EXHAUSTIVE_WHEN_ON_SEALED_CLASS.registerFactory(AddWhenElseBranchFix)
@@ -19,12 +19,18 @@ 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.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RemoveWhenElseBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhenEntry>(element) {
override fun getFamilyName() = KotlinBundle.message("remove.else.branch")
class RemoveWhenBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhenEntry>(element) {
override fun getFamilyName() = if (element?.isElse == true) {
KotlinBundle.message("remove.else.branch")
} else {
KotlinBundle.message("remove.branch")
}
override fun getText() = familyName
@@ -33,8 +39,15 @@ class RemoveWhenElseBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhe
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): RemoveWhenElseBranchFix? {
return (diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenElseBranchFix(it) }
override fun createAction(diagnostic: Diagnostic): RemoveWhenBranchFix? {
return when (diagnostic.factory) {
Errors.REDUNDANT_ELSE_IN_WHEN ->
(diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenBranchFix(it) }
Errors.SENSELESS_NULL_IN_WHEN ->
diagnostic.psiElement.getStrictParentOfType<KtWhenEntry>()?.let { RemoveWhenBranchFix(it) }
else ->
null
}
}
}
}
@@ -14042,6 +14042,11 @@ public abstract class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("testData/quickfix/when/noElseInWhenWithoutBranches.kt");
}
@TestMetadata("removeRedundantBranch.kt")
public void testRemoveRedundantBranch() throws Exception {
runTest("idea/testData/quickfix/when/removeRedundantBranch.kt");
}
@TestMetadata("removeRedundantElse.kt")
public void testRemoveRedundantElse() throws Exception {
runTest("testData/quickfix/when/removeRedundantElse.kt");
@@ -0,0 +1,9 @@
// "Remove branch" "true"
fun test(x: Int): String {
return when (x) {
1 -> "1"
2 -> "2"
<caret>null -> "null"
else -> ""
}
}
@@ -0,0 +1,8 @@
// "Remove branch" "true"
fun test(x: Int): String {
return when (x) {
1 -> "1"
2 -> "2"
else -> ""
}
}