IDEA-273932 - added highlighting for incorrect guarding expressions within guarded patterns

GitOrigin-RevId: 9ccee2cd8d848cc8ac1896eaa13781e4700e00d3
This commit is contained in:
Ilyas Selimov
2021-07-20 10:10:52 +00:00
committed by intellij-monorepo-bot
parent 2e784cc278
commit a9d3854aa5
3 changed files with 45 additions and 0 deletions
@@ -2026,6 +2026,16 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
public void visitGuardedPattern(PsiGuardedPattern pattern) {
super.visitGuardedPattern(pattern);
myHolder.add(checkFeature(pattern, HighlightingFeature.GUARDED_AND_PARENTHESIZED_PATTERNS));
if (myHolder.hasErrorResults()) return;
PsiExpression guardingExpr = pattern.getGuardingExpression();
if (guardingExpr == null) return;
// 14.30.1 Kinds of Patterns GuardedPattern: PrimaryPattern && ConditionalAndExpression
// 15.23. ConditionalAndExpression: Each operand of the conditional-and operator must be of type boolean or Boolean, or a compile-time error occurs.
if (!TypeConversionUtil.isBooleanType(guardingExpr.getType())) {
String message = JavaErrorBundle.message("incompatible.types", JavaHighlightUtil.formatType(PsiType.BOOLEAN),
JavaHighlightUtil.formatType(guardingExpr.getType()));
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(guardingExpr).descriptionAndTooltip(message).create());
}
}
@Override
@@ -0,0 +1,31 @@
class Test {
void test(Object o, Integer integer) {
switch (o) {
case String s && <error descr="Incompatible types. Found: 'java.lang.Integer', required: 'boolean'">integer</error> -> System.out.println();
default -> {}
}
switch (o) {
case String s && isBool() -> System.out.println();
default -> {}
}
switch (o) {
case Integer i && <error descr="Incompatible types. Found: 'int', required: 'boolean'">isInt()</error>:
break;
default:
break;
}
switch (o) {
case Integer i && <error descr="Incompatible types. Found: 'null', required: 'boolean'">null</error>:
break;
default:
break;
}
}
private native boolean isBool();
private native int isInt();
}
@@ -59,6 +59,10 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
doTest();
}
public void testGuardedPatterns() {
doTest();
}
public void testIdentifierHighlighterForPatternVariable() {
PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
PsiElement element = myFixture.getElementAtCaret();