diff --git a/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java b/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java index 172fd1aeb601..e4b421d597d5 100644 --- a/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/DuplicateBranchesInSwitchInspection.java @@ -126,10 +126,11 @@ public class DuplicateBranchesInSwitchInspection extends LocalInspectionTool { } private static boolean isMergeCasesFixAvailable(@NotNull BranchBase duplicate, @NotNull BranchBase original) { - if (duplicate.myIsPattern != original.myIsPattern) { + if (duplicate.myIsGuardedOrParenthesizedPattern || original.myIsGuardedOrParenthesizedPattern) return false; + if (duplicate.myIsTypeTestPattern != original.myIsTypeTestPattern) { return duplicate.myIsNull || original.myIsNull; } - return !duplicate.myIsPattern; + return !duplicate.myIsTypeTestPattern; } @@ -477,7 +478,8 @@ public class DuplicateBranchesInSwitchInspection extends LocalInspectionTool { protected final PsiStatement @NotNull [] myStatements; protected final String @NotNull [] myCommentTexts; private final boolean myIsDefault; - private final boolean myIsPattern; + private final boolean myIsTypeTestPattern; + private final boolean myIsGuardedOrParenthesizedPattern; private final boolean myIsNull; private DuplicatesFinder myFinder; @@ -489,28 +491,34 @@ public class DuplicateBranchesInSwitchInspection extends LocalInspectionTool { myStatements = statements; myCommentTexts = commentTexts; myIsDefault = ContainerUtil.find(labels, PsiSwitchLabelStatementBase::isDefaultCase) != null; - myIsPattern = isPattern(labels); + myIsTypeTestPattern = isPatternBy(labels, PsiTypeTestPattern.class); + myIsGuardedOrParenthesizedPattern = isPatternBy(labels, PsiGuardedPattern.class, PsiParenthesizedPattern.class); myIsNull = isNull(labels); } - private boolean isPattern(T @NotNull [] labels) { + private boolean isPatternBy(T @NotNull [] labels, Class... classes) { return ContainerUtil.find(labels, label -> { - PsiSwitchLabelStatementBase labelStatementBase = ObjectUtils.tryCast(label, PsiSwitchLabelStatementBase.class); - if (labelStatementBase == null) return false; - return PsiTreeUtil.getChildOfType(labelStatementBase.getCaseLabelElementList(), PsiPattern.class) != null; - }) != null; + PsiCaseLabelElement[] labelElements = getCaseLabelElements(label); + if (labelElements == null) return false; + return ContainerUtil.exists(labelElements, labelElement -> PsiTreeUtil.instanceOf(labelElement, classes)); + }) != null; } private boolean isNull(T @NotNull [] labels) { if (labels.length != 1) return false; - PsiSwitchLabelStatementBase labelStatement = ObjectUtils.tryCast(labels[0], PsiSwitchLabelStatementBase.class); - if (labelStatement == null) return false; - PsiCaseLabelElementList labelElementList = labelStatement.getCaseLabelElementList(); - if (labelElementList == null) return false; - PsiCaseLabelElement[] labelElements = labelElementList.getElements(); + PsiCaseLabelElement[] labelElements = getCaseLabelElements(labels[0]); + if (labelElements == null) return false; return labelElements.length == 1 && ExpressionUtils.isNullLiteral(ObjectUtils.tryCast(labelElements[0], PsiExpression.class)); } + private PsiCaseLabelElement[] getCaseLabelElements(T label) { + PsiSwitchLabelStatementBase labelStatementBase = ObjectUtils.tryCast(label, PsiSwitchLabelStatementBase.class); + if (labelStatementBase == null) return null; + PsiCaseLabelElementList labelElementList = labelStatementBase.getCaseLabelElementList(); + if (labelElementList == null) return null; + return labelElementList.getElements(); + } + boolean isDefault() { return myIsDefault; } diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/afterGuardedPatternMergeWithNull.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/afterGuardedPatternMergeWithNull.java deleted file mode 100644 index afe7cd2d59ef..000000000000 --- a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/afterGuardedPatternMergeWithNull.java +++ /dev/null @@ -1,11 +0,0 @@ -// "Merge with 'case null'" "GENERIC_ERROR_OR_WARNING" -class C { - void foo(Object o) { - switch (o) { - case null, Number n && n.intValue() == 42 -> bar("A"); - case String s -> bar("B"); - default -> bar("C"); - } - } - void bar(String s){} -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/afterNullMergeWithGuardedPattern.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/afterNullMergeWithGuardedPattern.java deleted file mode 100644 index 8ada44562123..000000000000 --- a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/afterNullMergeWithGuardedPattern.java +++ /dev/null @@ -1,11 +0,0 @@ -// "Merge with 'case Number n && n.intValue() == 42'" "GENERIC_ERROR_OR_WARNING" -class C { - void foo(Object o) { - switch (o) { - case Number n && n.intValue() == 42, null -> bar("A"); - case String s -> bar("B"); - default -> bar("C"); - } - } - void bar(String s){} -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeGuardedPatternMergeWithNull.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeGuardedPatternMergeWithNull.java index c9e92074316b..ee4f40219de4 100644 --- a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeGuardedPatternMergeWithNull.java +++ b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeGuardedPatternMergeWithNull.java @@ -1,4 +1,4 @@ -// "Merge with 'case null'" "GENERIC_ERROR_OR_WARNING" +// "Merge with 'case null'" "false" class C { void foo(Object o) { switch (o) { diff --git a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeNullMergeWithGuardedPattern.java b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeNullMergeWithGuardedPattern.java index 2b114328c24c..88e34477443b 100644 --- a/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeNullMergeWithGuardedPattern.java +++ b/java/java-tests/testData/inspection/duplicateBranchesInEnhancedSwitchFix/beforeNullMergeWithGuardedPattern.java @@ -1,4 +1,4 @@ -// "Merge with 'case Number n && n.intValue() == 42'" "GENERIC_ERROR_OR_WARNING" +// "Merge with 'case Number n && n.intValue() == 42'" "false" class C { void foo(Object o) { switch (o) {