mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] DuplicateBranchesInSwitch: warn on empty cases if a quick-fix is available
IDEA-291071 IJ-CR-95239 GitOrigin-RevId: 2fa8efa7057f75609b59f843a6fd7539919a3150
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9cc4d4bc41
commit
08756b6ced
+16
-2
@@ -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();
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
class C {
|
||||
void foo(int n) {
|
||||
switch (n) {
|
||||
case 1 -> {}
|
||||
case 2 -> {}
|
||||
case 3 -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class C {
|
||||
void foo(int n) {
|
||||
switch (n) {
|
||||
case 1 -> {}
|
||||
case 2 -> <weak_warning descr="Duplicate branch in 'switch'">{}</weak_warning>
|
||||
case 3 -> <weak_warning descr="Duplicate branch in 'switch'">{}</weak_warning>
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class C {
|
||||
void foo(Object obj) {
|
||||
switch (obj) {
|
||||
case String s -> {}
|
||||
case Object o -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user