From 5abb6bc7a54ed7b3536687f4448a073bc6a4b58d Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 4 Dec 2019 18:04:34 +0900 Subject: [PATCH] Add quickfix for SENSELESS_NULL_IN_WHEN to remove redundant when branch #KT-24556 Fixed --- .../messages/KotlinBundle.properties | 1 + .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 3 ++- ...lseBranchFix.kt => RemoveWhenBranchFix.kt} | 21 +++++++++++++++---- .../quickfix/when/removeRedundantBranch.kt | 9 ++++++++ .../when/removeRedundantBranch.kt.after | 8 +++++++ .../idea/quickfix/QuickFixTestGenerated.java | 5 +++++ 6 files changed, 42 insertions(+), 5 deletions(-) rename idea/src/org/jetbrains/kotlin/idea/quickfix/{RemoveWhenElseBranchFix.kt => RemoveWhenBranchFix.kt} (59%) create mode 100644 idea/testData/quickfix/when/removeRedundantBranch.kt create mode 100644 idea/testData/quickfix/when/removeRedundantBranch.kt.after diff --git a/idea/resources-en/messages/KotlinBundle.properties b/idea/resources-en/messages/KotlinBundle.properties index 9fc65d6f82d..8503a39be15 100644 --- a/idea/resources-en/messages/KotlinBundle.properties +++ b/idea/resources-en/messages/KotlinBundle.properties @@ -1138,6 +1138,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 diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 2c543b40cd6..4a5a367c68f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -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) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenBranchFix.kt similarity index 59% rename from idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt rename to idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenBranchFix.kt index 61f4f491807..85971b93a8f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenBranchFix.kt @@ -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(element) { - override fun getFamilyName() = KotlinBundle.message("remove.else.branch") +class RemoveWhenBranchFix(element: KtWhenEntry) : KotlinQuickFixAction(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 + (diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenBranchFix(it) } + Errors.SENSELESS_NULL_IN_WHEN -> + diagnostic.psiElement.getStrictParentOfType()?.let { RemoveWhenBranchFix(it) } + else -> + null + } } } } diff --git a/idea/testData/quickfix/when/removeRedundantBranch.kt b/idea/testData/quickfix/when/removeRedundantBranch.kt new file mode 100644 index 00000000000..c76cf65a675 --- /dev/null +++ b/idea/testData/quickfix/when/removeRedundantBranch.kt @@ -0,0 +1,9 @@ +// "Remove branch" "true" +fun test(x: Int): String { + return when (x) { + 1 -> "1" + 2 -> "2" + null -> "null" + else -> "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/removeRedundantBranch.kt.after b/idea/testData/quickfix/when/removeRedundantBranch.kt.after new file mode 100644 index 00000000000..08cb050bc84 --- /dev/null +++ b/idea/testData/quickfix/when/removeRedundantBranch.kt.after @@ -0,0 +1,8 @@ +// "Remove branch" "true" +fun test(x: Int): String { + return when (x) { + 1 -> "1" + 2 -> "2" + else -> "" + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 1bc5694ae97..b9fa6d427b2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -15517,6 +15517,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { runTest("idea/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("idea/testData/quickfix/when/removeRedundantElse.kt");