Java: report primitive patterns in switch after case null branch correctly (IDEA-374619)

GitOrigin-RevId: 3afe71cd1d81188830b1cc332689f42c33e8da8b
This commit is contained in:
Bas Leijdekkers
2025-07-09 10:20:35 +00:00
committed by intellij-monorepo-bot
parent aa7f2804fb
commit 17abd817b0
2 changed files with 19 additions and 6 deletions
@@ -294,13 +294,10 @@ final class SwitchChecker {
if (labelElementList == null) continue;
for (PsiCaseLabelElement label : labelElementList.getElements()) {
if (!(label instanceof PsiParenthesizedExpression) && ExpressionUtil.isNullLiteral(label)) {
if (selectorType instanceof PsiPrimitiveType) {
if (!PsiTypes.nullType().equals(selectorType)) {
myVisitor.report(JavaErrorKinds.SWITCH_NULL_TYPE_INCOMPATIBLE.create(label, selectorType));
return;
}
if (selectorType instanceof PsiPrimitiveType && !PsiTypes.nullType().equals(selectorType)) {
myVisitor.report(JavaErrorKinds.SWITCH_NULL_TYPE_INCOMPATIBLE.create(label, selectorType));
}
return;
continue;
}
if (label instanceof PsiExpression expr) {
if (selectorType.equals(PsiTypes.nullType())) {
@@ -27,6 +27,22 @@ class X {
};
}
boolean method(Long l) {
return switch (l) {
case null -> false;
case <error descr="Primitive types in patterns, instanceof and switch are not supported at language level '21'">long ll</error> when (ll & 1) > 0 -> true;
default -> false;
};
}
boolean method(int l) {
return switch (l) {
case <error descr="'null' cannot be converted to 'int'">null</error> -> false;
case <error descr="Primitive types in patterns, instanceof and switch are not supported at language level '21'">long ll</error> when (ll & 1) > 0 -> true;
default -> false;
};
}
void switchTest3(<error descr="Cannot resolve symbol 'Point'">Point</error><? extends String> point1, <error descr="Cannot resolve symbol 'Point'">Point</error><? super String> point2) {
switch (point1) {
case <error descr="Cannot resolve symbol 'Point'">Point</error><?>() -> {}