IDEA-77484 + tests

This commit is contained in:
Maxim Shafirov
2012-03-15 19:13:32 +04:00
parent b0ec6b4c4a
commit f4e9d475a3
4 changed files with 34 additions and 2 deletions
@@ -16,6 +16,7 @@
package com.intellij.codeInspection.dataFlow;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.codeInspection.dataFlow.instructions.*;
import com.intellij.codeInspection.dataFlow.value.DfaUnknownValue;
import com.intellij.codeInspection.dataFlow.value.DfaValue;
@@ -1431,6 +1432,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
startElement(expression);
DfaValue dfaValue = myFactory.create(expression);
PsiElement resolved = expression.resolve();
if (dfaValue instanceof DfaVariableValue) {
DfaVariableValue dfaVariable = (DfaVariableValue)dfaValue;
PsiVariable psiVariable = dfaVariable.getPsiVariable();
@@ -1442,7 +1444,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
final PsiExpression qualifierExpression = expression.getQualifierExpression();
if (qualifierExpression != null) {
qualifierExpression.accept(this);
if (expression.resolve() instanceof PsiField) {
if (resolved instanceof PsiField) {
addInstruction(new FieldReferenceInstruction(expression, null));
}
else {
@@ -1450,6 +1452,12 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
}
}
if (dfaValue == null && resolved instanceof PsiField) {
// Accessing a field from another instance
dfaValue = myFactory.getTypeFactory().create(((PsiField)resolved).getType(),
NullableNotNullManager.isNullable((PsiModifierListOwner)resolved));
}
addInstruction(new PushInstruction(dfaValue, expression));
finishElement(expression);
@@ -0,0 +1,14 @@
import org.jetbrains.annotations.Nullable;
public class DDD {
int test(boolean t) {
if (t && <warning descr="Dereference of 'fff()' may produce 'java.lang.NullPointerException'">fff()</warning>.length == 1) {
return 0;
}
return 1;
}
public @Nullable DDD[] fff() {
return new DDD[8];
}
}
@@ -0,0 +1,9 @@
import org.jetbrains.annotations.Nullable;
public class DDD {
@Nullable
String field;
int test() {
return <warning descr="Method invocation 'new DDD().field.hashCode()' may produce 'java.lang.NullPointerException'">new DDD().field.hashCode()</warning>;
}
}
@@ -33,5 +33,6 @@ public class DataFlowInspectionFixtureTest extends JavaCodeInsightFixtureTestCas
public void testNullableAnonymousVolatileNotNull() throws Throwable { doTest(); }
public void testFieldInAnonymous() throws Throwable { doTest(); }
public void testNullableField() throws Throwable { doTest(); }
public void testCanBeNullDoesntImplyIsNull() throws Throwable { doTest(); }
}