diff --git a/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties b/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties index 29fdd0af9a25..c7a2c80af738 100644 --- a/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties +++ b/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties @@ -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 diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index f149652d31d2..52b0bdd596ac 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/plugins/kotlin/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/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenBranchFix.kt similarity index 59% rename from plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt rename to plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenBranchFix.kt index 61f4f491807d..85971b93a8f4 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt +++ b/plugins/kotlin/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/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 403c8cf202ec..1fcc7c7b4ae3 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -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"); diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/removeRedundantBranch.kt b/plugins/kotlin/idea/tests/testData/quickfix/when/removeRedundantBranch.kt new file mode 100644 index 000000000000..c76cf65a6751 --- /dev/null +++ b/plugins/kotlin/idea/tests/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/plugins/kotlin/idea/tests/testData/quickfix/when/removeRedundantBranch.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/when/removeRedundantBranch.kt.after new file mode 100644 index 000000000000..08cb050bc84b --- /dev/null +++ b/plugins/kotlin/idea/tests/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