[java-highlighting] IDEA-366905 'Label is dominated by a preceding case label' error highlighting appears in a case when there is no compilation error

GitOrigin-RevId: a6cb61a29153fbd0524b15995427cd2dc285c653
This commit is contained in:
Mikhail Pyltsin
2025-02-11 11:58:17 +00:00
committed by intellij-monorepo-bot
parent 1fda15509a
commit 26a09a1c53
4 changed files with 17 additions and 11 deletions
@@ -375,8 +375,6 @@ public class PatternsInSwitchBlockHighlightingModel extends SwitchBlockHighlight
@NotNull PsiElement who,
@NotNull PsiType selectorType) {
boolean isOverWhomUnconditionalForSelector = JavaPsiPatternUtil.isUnconditionalForType(overWhom, selectorType);
boolean isWhoUnconditionalForSelector = who instanceof PsiCaseLabelElement whoCase &&
JavaPsiPatternUtil.isUnconditionalForType(whoCase, selectorType);
if (!isOverWhomUnconditionalForSelector &&
((!(overWhom instanceof PsiExpression expression) || ExpressionUtils.isNullLiteral(expression)) &&
who instanceof PsiKeyword &&
@@ -387,12 +385,6 @@ public class PatternsInSwitchBlockHighlightingModel extends SwitchBlockHighlight
// A 'case null, default' label dominates all other switch labels.
return true;
}
if (isWhoUnconditionalForSelector && !isOverWhomUnconditionalForSelector &&
!(isInCaseNullDefaultLabel(overWhom) ||
(overWhom instanceof PsiKeyword && PsiKeyword.DEFAULT.equals(overWhom.getText())) ||
(overWhom instanceof PsiExpression expression && ExpressionUtils.isNullLiteral(expression)))) {
return true;
}
if (who instanceof PsiCaseLabelElement currentElement) {
if (JavaPsiPatternUtil.isGuarded(currentElement)) return false;
if (isConstantLabelElement(overWhom)) {
@@ -8,7 +8,7 @@ public class UnconditionalForSelectTypeAndDominated {
private static void testAA(AA aa){
switch (aa) {
case AA1 aa1 -> System.out.println(1);
case <error descr="Label is dominated by a preceding case label 'AA1 aa1'">B aa1</error> -> System.out.println(1);
case B aa1 -> System.out.println(1);
}
}
}
@@ -1202,4 +1202,18 @@ public class SwitchPrimitivePatternDominated {
case <error descr="Label is dominated by a preceding case label 'Object b'">boolean b</error> -> System.out.println("2");//error
}
}
static void switchInteger(Integer i) {
switch (i) {
case Integer i2 -> System.out.println("int" + i2);
case long l1 -> System.out.println("long" + l1);
}
}
static void switchDouble(double i) {
switch (i) {
case double i2 -> System.out.println("double" + i2);
case long l1 -> System.out.println("long" + l1);
}
}
}
@@ -172,8 +172,8 @@ public class SwitchPrimitivePatternList {
case short d -> System.out.println("1");
case int d -> System.out.println("1");
case long d -> System.out.println("1");
case <error descr="Label is dominated by a preceding case label 'long d'">float d</error> -> System.out.println("1");
case <error descr="Label is dominated by a preceding case label 'long d'">double d</error> -> System.out.println("1");
case float d -> System.out.println("1");
case double d -> System.out.println("1");
}
}