IDEA-290334 - fix red code warnings regarding patterns in switch/instanceof for older java versions

GitOrigin-RevId: 9656a5b1db7f35788b8acaad4659804fe2305660
This commit is contained in:
Vladimir.Petrenko
2022-03-14 13:45:43 +03:00
committed by intellij-monorepo-bot
parent c4937c5ce4
commit d7792746d6
4 changed files with 40 additions and 6 deletions

View File

@@ -1774,7 +1774,14 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
public void visitVariable(PsiVariable variable) {
super.visitVariable(variable);
if (variable instanceof PsiPatternVariable) {
myHolder.add(checkFeature(((PsiPatternVariable)variable).getNameIdentifier(), HighlightingFeature.PATTERNS));
PsiElement declarationScope = ((PsiPatternVariable)variable).getDeclarationScope().getParent();
PsiIdentifier varIdentifier = ((PsiPatternVariable)variable).getNameIdentifier();
if (declarationScope instanceof PsiSwitchBlock) {
myHolder.add(checkFeature(varIdentifier, HighlightingFeature.PATTERNS_IN_SWITCH));
}
else {
myHolder.add(checkFeature(varIdentifier, HighlightingFeature.PATTERNS));
}
}
try {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkVarTypeApplicability(variable));

View File

@@ -0,0 +1,23 @@
class X {
int switchTest(Object obj) {
return switch (obj) {
case (String <error descr="Patterns in switch are not supported at language level '11'">s</error>) -> 1;
case Integer <error descr="Patterns in switch are not supported at language level '11'">i</error> && predicate() -> 2;
case Integer <error descr="Patterns in switch are not supported at language level '11'">i</error> -> 3;
case <error descr="Patterns in switch are not supported at language level '11'">default</error> -> 4;
case <error descr="Patterns in switch are not supported at language level '11'">null</error> -> 10;
};
}
int instanceofTest(Object obj) {
if (obj instanceof (Integer <error descr="Patterns in 'instanceof' are not supported at language level '11'">i</error> && predicate())) {
return 1;
}
if (obj instanceof (String <error descr="Patterns in 'instanceof' are not supported at language level '11'">s</error>)) {
return 3;
}
return 2;
}
native static boolean predicate();
}

View File

@@ -1,9 +1,9 @@
class X {
int switchTest(Object obj) {
return switch (obj) {
case <error descr="Guarded and parenthesized patterns are not supported at language level '16'">(String s)</error> -> 1;
case <error descr="Guarded and parenthesized patterns are not supported at language level '16'">Integer i && predicate()</error> -> 2;
case <error descr="Patterns in switch are not supported at language level '16'">Integer i</error> -> 3;
case (String <error descr="Patterns in switch are not supported at language level '16'">s</error>) -> 1;
case Integer <error descr="Patterns in switch are not supported at language level '16'">i</error> && predicate() -> 2;
case Integer <error descr="Patterns in switch are not supported at language level '16'">i</error> -> 3;
case <error descr="Patterns in switch are not supported at language level '16'">default</error> -> 4;
case <error descr="Patterns in switch are not supported at language level '16'">null</error> -> 10;
};

View File

@@ -25,8 +25,12 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
return JAVA_17;
}
public void testPatternsInSwitchInOldJava() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_16, this::doTest);
public void testPatternsInSwitchIn16Java() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_16, this::doTest);
}
public void testPatternsInSwitchIn11Java() {
IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_11, this::doTest);
}
public void testPatternMatchingInSwitch() {