From c26d7e0ebad6646f14c2b4940b6708109a44164c Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 27 May 2016 13:04:12 +0300 Subject: [PATCH] Attach "add remaining branches" and "add else branch" fixes to NON_EXHAUSTIVE_WHEN warning #KT-12503 Fixed --- .../src/org/jetbrains/kotlin/cfg/WhenChecker.kt | 2 +- .../idea/quickfix/AddWhenRemainingBranchesFix.kt | 11 +++++------ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 ++ .../quickfix/when/addElseBranchEnumStatement.kt | 9 +++++++++ .../when/addElseBranchEnumStatement.kt.after | 11 +++++++++++ .../when/addRemainingBranchesEnumStatement.kt | 11 +++++++++++ .../when/addRemainingBranchesEnumStatement.kt.after | 13 +++++++++++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 12 ++++++++++++ 8 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 idea/testData/quickfix/when/addElseBranchEnumStatement.kt create mode 100644 idea/testData/quickfix/when/addElseBranchEnumStatement.kt.after create mode 100644 idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt create mode 100644 idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt.after diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/WhenChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/WhenChecker.kt index ba409d221f4..b65f2ebfa4f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/WhenChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/WhenChecker.kt @@ -321,7 +321,7 @@ object WhenChecker { return true } - private fun getMissingCases(expression: KtWhenExpression, context: BindingContext): List { + fun getMissingCases(expression: KtWhenExpression, context: BindingContext): List { val type = whenSubjectType(expression, context) ?: return listOf(UnknownMissingCase) val nullable = !type.isFlexible() && isNullableTypeWithoutPossibleSmartCast(expression.subjectExpression, type, context) val checkers = exhaustivenessCheckers.filter { it.isApplicable(type) } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenRemainingBranchesFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenRemainingBranchesFix.kt index 0509c8c9c85..f41befac058 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenRemainingBranchesFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenRemainingBranchesFix.kt @@ -37,25 +37,24 @@ class AddWhenRemainingBranchesFix(expression: KtWhenExpression) : KotlinQuickFix override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean = super.isAvailable(project, editor, file) && element.closeBrace != null && - with(WhenChecker.getNecessaryCases(element, element.analyze())) { + with(WhenChecker.getMissingCases(element, element.analyze())) { isNotEmpty() && !hasUnknown } override fun invoke(project: Project, editor: Editor?, file: KtFile) { - val necessaryCases = WhenChecker.getNecessaryCases(element, element.analyze()) + val missingCases = WhenChecker.getMissingCases(element, element.analyze()) - val whenCloseBrace = element.closeBrace - assert(whenCloseBrace != null) { "isAvailable should check if close brace exist" } + val whenCloseBrace = element.closeBrace ?: throw AssertionError("isAvailable should check if close brace exist") val psiFactory = KtPsiFactory(file) - for (case in necessaryCases) { + for (case in missingCases) { val entry = psiFactory.createWhenEntry("${case.branchConditionText} -> TODO()") element.addBefore(entry, whenCloseBrace) } } companion object : KotlinSingleIntentionActionFactory() { - override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { val whenExpression = diagnostic.psiElement.getNonStrictParentOfType() ?: return null return AddWhenRemainingBranchesFix(whenExpression) } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 72e73a75736..1555fe76e45 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -201,6 +201,8 @@ class QuickFixRegistrar : QuickFixContributor { ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix) NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix) NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix) + NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenElseBranchFix) + NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenRemainingBranchesFix) BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix) NO_TYPE_ARGUMENTS_ON_RHS.registerFactory(AddStarProjectionsFix.IsExpressionFactory) diff --git a/idea/testData/quickfix/when/addElseBranchEnumStatement.kt b/idea/testData/quickfix/when/addElseBranchEnumStatement.kt new file mode 100644 index 00000000000..732d403cc3e --- /dev/null +++ b/idea/testData/quickfix/when/addElseBranchEnumStatement.kt @@ -0,0 +1,9 @@ +// "Add else branch" "true" +enum class Color { R, G, B } +fun use(c: Color) { + when (c) { + Color.R -> red() + } +} + +fun red() {} diff --git a/idea/testData/quickfix/when/addElseBranchEnumStatement.kt.after b/idea/testData/quickfix/when/addElseBranchEnumStatement.kt.after new file mode 100644 index 00000000000..38e5805593c --- /dev/null +++ b/idea/testData/quickfix/when/addElseBranchEnumStatement.kt.after @@ -0,0 +1,11 @@ +// "Add else branch" "true" +enum class Color { R, G, B } +fun use(c: Color) { + when (c) { + Color.R -> red() + else -> { + } + } +} + +fun red() {} diff --git a/idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt b/idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt new file mode 100644 index 00000000000..ee6ebd049f8 --- /dev/null +++ b/idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt @@ -0,0 +1,11 @@ +// "Add remaining branches" "true" +// ERROR: Unresolved reference: TODO +// ERROR: Unresolved reference: TODO +enum class Color { R, G, B } +fun use(c: Color) { + when (c) { + Color.R -> red() + } +} + +fun red() {} diff --git a/idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt.after b/idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt.after new file mode 100644 index 00000000000..e473a6374c9 --- /dev/null +++ b/idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt.after @@ -0,0 +1,13 @@ +// "Add remaining branches" "true" +// ERROR: Unresolved reference: TODO +// ERROR: Unresolved reference: TODO +enum class Color { R, G, B } +fun use(c: Color) { + when (c) { + Color.R -> red() + Color.G -> TODO() + Color.B -> TODO() + } +} + +fun red() {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 92904c79fc6..903776ca2ae 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -8532,6 +8532,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class When extends AbstractQuickFixTest { + @TestMetadata("addElseBranchEnumStatement.kt") + public void testAddElseBranchEnumStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/addElseBranchEnumStatement.kt"); + doTest(fileName); + } + @TestMetadata("addRemainingBranchesBoolean.kt") public void testAddRemainingBranchesBoolean() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/addRemainingBranchesBoolean.kt"); @@ -8544,6 +8550,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("addRemainingBranchesEnumStatement.kt") + public void testAddRemainingBranchesEnumStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/addRemainingBranchesEnumStatement.kt"); + doTest(fileName); + } + @TestMetadata("addRemainingBranchesInNonDefaultPackage.kt") public void testAddRemainingBranchesInNonDefaultPackage() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/addRemainingBranchesInNonDefaultPackage.kt");