[java-dfa] getArrayElementValue: fix unboxing Object[] arrays

Fixes IDEA-288566 Wrong warning at array element instanceof when it was autoboxed

GitOrigin-RevId: f4c1a4bceca84db8842541ed0468af8785a839c7
This commit is contained in:
Tagir Valeev
2022-02-14 13:37:50 +00:00
committed by intellij-monorepo-bot
parent 6754c44959
commit b2fd2611de
3 changed files with 14 additions and 0 deletions
@@ -97,6 +97,12 @@ public final class ArrayElementDescriptor extends JvmVariableDescriptor {
if (arrayPsiVar != null) {
PsiExpression constantArrayElement = ExpressionUtils.getConstantArrayElement(arrayPsiVar, index);
if (constantArrayElement != null) {
PsiType elementType = constantArrayElement.getType();
if (componentType instanceof DfReferenceType && elementType instanceof PsiPrimitiveType &&
!TypeConstraint.fromDfType(componentType).isPrimitiveWrapper()) {
componentType = DfTypes.typedObject(((PsiPrimitiveType)elementType).getBoxedType(constantArrayElement), Nullability.UNKNOWN)
.meet(componentType);
}
return getAdvancedExpressionDfaValue(factory, constantArrayElement, componentType);
}
}
@@ -0,0 +1,7 @@
public class BoxingInArrayDeclaration {
public static void main(String[] args) {
Object[] arr = {1, 2};
System.out.println(<warning descr="Condition 'arr[0] instanceof Integer' is always 'true'">arr[0] instanceof Integer</warning>);
}
}
@@ -718,4 +718,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testEqualsTwoFields() { doTest();}
public void testPureMethodReadsMutableArray() { doTest(); }
public void testBoxingInConstructorArguments() { doTest(); }
public void testBoxingInArrayDeclaration() { doTest(); }
}