java switch expressions: infer Object if one of branches has null type (IDEA-241470)

GitOrigin-RevId: 489d3413091cbd1a85348fae0a428e29b287f7e0
This commit is contained in:
Anna Kozlova
2020-05-25 21:31:52 +02:00
committed by intellij-monorepo-bot
parent 6324dc91a2
commit 8aafb282f3
2 changed files with 10 additions and 1 deletions

View File

@@ -92,10 +92,11 @@ public class PsiSwitchExpressionImpl extends PsiSwitchBlockImpl implements PsiSw
// to the least upper bound (4.10.4) of the types of the result expressions.
PsiType leastUpperBound = PsiType.NULL;
for (PsiType type : resultTypes) {
if (TypeConversionUtil.isNullType(type)) return PsiType.getJavaLangObject(getManager(), getResolveScope());
if (TypeConversionUtil.isPrimitiveAndNotNull(type)) {
type = ((PsiPrimitiveType)type).getBoxedType(this);
}
if (leastUpperBound == PsiType.NULL) {
if (TypeConversionUtil.isNullType(leastUpperBound)) {
leastUpperBound = type;
}
else {

View File

@@ -116,4 +116,12 @@ class MyTest {
System.out.println(c.getCanonicalName());
}
static void testNull(int i) {
var v = switch(i) {
case 1 -> "abcd";
default -> null;
};
System.out.println(v.<error descr="Cannot resolve method 'substring' in 'Object'">substring</error>(1));
}
}