IDEA-244356 Constant condition incorrect when declaring a list of integers with wildcard

GitOrigin-RevId: 0fad4814936b86fcf361f0245be66b98483a7959
This commit is contained in:
Tagir Valeev
2020-06-25 14:08:48 +03:00
committed by intellij-monorepo-bot
parent b573f79a61
commit 7fdfffcc0b
3 changed files with 26 additions and 2 deletions
@@ -1448,11 +1448,13 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
void generateBoxingUnboxingInstructionFor(@NotNull PsiExpression context, PsiType actualType, PsiType expectedType, boolean explicit) {
if (PsiType.VOID.equals(expectedType)) return;
if (TypeConversionUtil.isPrimitiveAndNotNull(expectedType) && TypeConversionUtil.isAssignableFromPrimitiveWrapper(actualType)) {
if (TypeConversionUtil.isPrimitiveAndNotNull(expectedType) &&
TypeConversionUtil.isAssignableFromPrimitiveWrapper(GenericsUtil.getVariableTypeByExpressionType(actualType))) {
addInstruction(new UnwrapSpecialFieldInstruction(SpecialField.UNBOX));
actualType = PsiPrimitiveType.getUnboxedType(actualType);
}
if (TypeConversionUtil.isPrimitiveAndNotNull(actualType) && TypeConversionUtil.isAssignableFromPrimitiveWrapper(expectedType)) {
if (TypeConversionUtil.isPrimitiveAndNotNull(actualType) &&
TypeConversionUtil.isAssignableFromPrimitiveWrapper(GenericsUtil.getVariableTypeByExpressionType(expectedType))) {
addConditionalErrorThrow();
PsiType boxedType = ((PsiPrimitiveType)actualType).getBoxedType(context);
addInstruction(new BoxingInstruction(boxedType));
@@ -0,0 +1,21 @@
import org.jetbrains.annotations.Nullable;
import java.util.*;
// IDEA-244356
class Usage {
private static void exampleMethod(List<? extends Integer> listOfIntegers) {
int firstElement = listOfIntegers.get(0);
for (int element : listOfIntegers) {
if (element == firstElement) {}
}
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
exampleMethod(list);
}
}
@@ -624,6 +624,7 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testNewBoxedNumberEquality() { doTest(); }
public void testBoxingIncorrectLiteral() { doTest(); }
public void testImplicitUnboxingOnCast() { doTest(); }
public void testImplicitUnboxingExtendsInteger() { doTest(); }
public void testIncompleteArrayAccessInLoop() { doTest(); }
public void testSameArguments() { doTest(); }