IDEA-139253 TYPE_USE @Nullable-Annotations don't work correctly with arrays

This commit is contained in:
peter
2015-04-16 19:11:46 +03:00
parent 10560f057f
commit ce9914e697
5 changed files with 47 additions and 10 deletions
@@ -212,7 +212,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
private void flushArrayElementsOnUnknownIndexAssignment(PsiExpression lExpr) {
if (lExpr instanceof PsiArrayAccessExpression &&
myFactory.createValue(lExpr) == null // check for unknown index, otherwise AssignInstruction will flush only that element
!(myFactory.createValue(lExpr) instanceof DfaVariableValue) // check for unknown index, otherwise AssignInstruction will flush only that element
) {
DfaValue arrayVar = myFactory.createValue(((PsiArrayAccessExpression)lExpr).getArrayExpression());
if (arrayVar instanceof DfaVariableValue) {
@@ -70,13 +70,6 @@ public class DfaPsiUtil {
return Nullness.UNKNOWN;
}
if (NullableNotNullManager.isNullable(owner)) {
return Nullness.NULLABLE;
}
if (NullableNotNullManager.isNotNull(owner)) {
return Nullness.NOT_NULL;
}
if (resultType != null) {
NullableNotNullManager nnn = NullableNotNullManager.getInstance(owner.getProject());
for (PsiAnnotation annotation : resultType.getAnnotations()) {
@@ -95,6 +88,13 @@ public class DfaPsiUtil {
}
}
if (NullableNotNullManager.isNullable(owner)) {
return Nullness.NULLABLE;
}
if (NullableNotNullManager.isNotNull(owner)) {
return Nullness.NOT_NULL;
}
return Nullness.UNKNOWN;
}
@@ -70,6 +70,7 @@ public class DfaExpressionFactory {
return getExpressionDfaValue(((PsiParenthesizedExpression)expression).getExpression());
}
PsiType type = expression.getType();
if (expression instanceof PsiArrayAccessExpression) {
PsiExpression arrayExpression = ((PsiArrayAccessExpression)expression).getArrayExpression();
DfaValue qualifier = getExpressionDfaValue(arrayExpression);
@@ -79,7 +80,9 @@ public class DfaExpressionFactory {
return myFactory.getVarFactory().createVariableValue(indexVar, expression.getType(), false, (DfaVariableValue)qualifier);
}
}
return null;
if (type != null) {
return myFactory.createTypeValue(type, DfaPsiUtil.getElementNullability(type, null));
}
}
if (expression instanceof PsiMethodCallExpression) {
@@ -99,7 +102,6 @@ public class DfaExpressionFactory {
}
final Object value = JavaConstantExpressionEvaluator.computeConstantExpression(expression, false);
PsiType type = expression.getType();
if (value != null && type != null) {
if (value instanceof String) {
return myFactory.createTypeValue(type, Nullness.NOT_NULL); // Non-null string literal.
@@ -0,0 +1,27 @@
import foo.*;
import java.util.List;
class TestCompilerWarnings {
public void m(@NotNull Object x) {
assert x != null;
}
public void test1Array(@Nullable String @NotNull [] x) {
if (<warning descr="Condition 'x == null' is always 'false'">x == null</warning>) {
System.out.println("x is null");
}
m(x);
m(<warning descr="Argument 'x[0]' might be null">x[0]</warning>);
}
public void test2Array(@NotNull String @Nullable [] x) {
if (x == null) {
System.out.println("x is null");
} else {
m(x[0]);
}
m(<warning descr="Argument 'x' might be null">x</warning>);
}
}
@@ -83,4 +83,12 @@ public class DataFlowInspection8Test extends LightCodeInsightFixtureTestCase {
setupCustomAnnotations();
doTest();
}
public void testNullableArrayComponent() {
setupCustomAnnotations();
final DataFlowInspection inspection = new DataFlowInspection();
inspection.IGNORE_ASSERT_STATEMENTS = true;
myFixture.enableInspections(inspection);
myFixture.testHighlighting(true, false, true, getTestName(false) + ".java");
}
}