diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index b86ddcb1432..b4a5b45b8af 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -238,6 +238,7 @@ 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) NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenElseBranchFix) NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenRemainingBranchesFix) BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt new file mode 100644 index 00000000000..b27d90f6978 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveWhenElseBranchFix.kt @@ -0,0 +1,39 @@ +/* + * 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.psi.KtFile +import org.jetbrains.kotlin.psi.KtWhenEntry + +class RemoveWhenElseBranchFix(element: KtWhenEntry) : KotlinQuickFixAction(element) { + override fun getFamilyName() = "Remove else branch" + + override fun getText() = familyName + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + element?.delete() + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): RemoveWhenElseBranchFix? { + return (diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenElseBranchFix(it) } + } + } +} diff --git a/idea/testData/quickfix/when/removeRedundantElse.kt b/idea/testData/quickfix/when/removeRedundantElse.kt new file mode 100644 index 00000000000..d9e65c9e7f0 --- /dev/null +++ b/idea/testData/quickfix/when/removeRedundantElse.kt @@ -0,0 +1,9 @@ +// "Remove else branch" "true" + +fun foo(b: Boolean) { + when (b) { + true -> return + false -> {} + else -> error() + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/removeRedundantElse.kt.after b/idea/testData/quickfix/when/removeRedundantElse.kt.after new file mode 100644 index 00000000000..0bec99fd674 --- /dev/null +++ b/idea/testData/quickfix/when/removeRedundantElse.kt.after @@ -0,0 +1,8 @@ +// "Remove else branch" "true" + +fun foo(b: Boolean) { + when (b) { + true -> return + false -> {} + } +} \ 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 cd45a3fb58c..1879bff92c1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -10886,6 +10886,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("removeRedundantElse.kt") + public void testRemoveRedundantElse() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/removeRedundantElse.kt"); + doTest(fileName); + } + @TestMetadata("twoElseBranchesInWhen.kt") public void testTwoElseBranchesInWhen() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/twoElseBranchesInWhen.kt");