diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java index 2a1afab8926f..519adf9293ce 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java @@ -134,7 +134,9 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo private void highlightDuplicate(@NotNull BranchBase duplicate, @NotNull BranchBase original) { LocalQuickFix fix = isMergeCasesFixAvailable(duplicate, original) ? original.newMergeCasesFix() : null; - registerProblem(duplicate, duplicate.getCaseBranchMessage(), fix); + if (fix != null || !duplicate.isEmptyRuleBody()) { + registerProblem(duplicate, duplicate.getCaseBranchMessage(), fix); + } } private void highlightDefaultDuplicate(@NotNull BranchBase branch) { @@ -166,7 +168,7 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo } PsiSwitchLabeledRuleStatement ruleStatement = (PsiSwitchLabeledRuleStatement)element; PsiStatement body = ruleStatement.getBody(); - if (body != null && !(body instanceof PsiBlockStatement blockStatement && blockStatement.getCodeBlock().isEmpty())) { + if (body != null) { TryWithIdenticalCatchesInspection.collectCommentTexts(ruleStatement, commentTexts); Rule rule = new Rule(ruleStatement, body, ArrayUtilRt.toStringArray(commentTexts)); commentTexts.clear(); @@ -571,6 +573,8 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo abstract LocalQuickFix newMergeWithDefaultFix(); + abstract boolean isEmptyRuleBody(); + @Nullable Match match(BranchBase other) { return getFinder().isDuplicate(other.myStatements[0], true); @@ -729,6 +733,11 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo return new MergeWithDefaultBranchFix(); } + @Override + boolean isEmptyRuleBody() { + return false; + } + @Override LocalQuickFix newDeleteCaseFix() { return new DeleteRedundantBranchFix(); @@ -866,6 +875,11 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo return null; } + @Override + boolean isEmptyRuleBody() { + return myStatements[0] instanceof PsiBlockStatement blockStatement && blockStatement.getCodeBlock().isEmpty(); + } + @Override LocalQuickFix newDeleteCaseFix() { return new DeleteRedundantRuleFix(); diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodies.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodies.java deleted file mode 100644 index 249fe0f4a49d..000000000000 --- a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodies.java +++ /dev/null @@ -1,9 +0,0 @@ -class C { - void foo(int n) { - switch (n) { - case 1 -> {} - case 2 -> {} - case 3 -> {} - } - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodiesCanBeMerge.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodiesCanBeMerge.java new file mode 100644 index 000000000000..1694417ed9e2 --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodiesCanBeMerge.java @@ -0,0 +1,9 @@ +class C { + void foo(int n) { + switch (n) { + case 1 -> {} + case 2 -> {} + case 3 -> {} + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodiesCannotBeMerge.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodiesCannotBeMerge.java new file mode 100644 index 000000000000..261e9987516e --- /dev/null +++ b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitch/EmptyBodiesCannotBeMerge.java @@ -0,0 +1,8 @@ +class C { + void foo(Object obj) { + switch (obj) { + case String s -> {} + case Object o -> {} + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInEnhancedSwitchTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInEnhancedSwitchTest.kt index 6a58090f9051..3b48f4c450cd 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInEnhancedSwitchTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DuplicateBranchesInEnhancedSwitchTest.kt @@ -3,6 +3,7 @@ package com.intellij.java.codeInspection import com.intellij.JavaTestUtil import com.intellij.codeInspection.DuplicateBranchesInSwitchInspection +import com.intellij.testFramework.LightProjectDescriptor import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase /** @@ -23,10 +24,15 @@ class DuplicateBranchesInEnhancedSwitchTest : LightJavaCodeInsightFixtureTestCas fun testCaseLabelsExpressionDefaultLast() = doTest() fun testCaseLabelsExpressionDifferentComments() = doTest() fun testCaseLabelsExpressionSameComments() = doTest() - fun testEmptyBodies() = doTest() + fun testEmptyBodiesCanBeMerge() = doTest() + fun testEmptyBodiesCannotBeMerge() = doTest() private fun doTest() { myFixture.enableInspections(DuplicateBranchesInSwitchInspection()) myFixture.testHighlighting("${getTestName(false)}.java") } + + override fun getProjectDescriptor(): LightProjectDescriptor { + return JAVA_17 + } } \ No newline at end of file