IDEA-136603 IDEA does not warn if annotated with @Nullable local variable is used for @NotNull parameter method

This commit is contained in:
peter
2015-02-17 15:55:07 +01:00
parent f1e657e0a3
commit fb5ddff5cf
5 changed files with 29 additions and 5 deletions
@@ -1330,11 +1330,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
@Override public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
startElement(expression);
PsiElement[] children = expression.getChildren();
for (PsiElement child : children) {
child.accept(this);
}
pushUnknown();
addInstruction(new PushInstruction(myFactory.createTypeValue(expression.getType(), Nullness.NOT_NULL), expression));
finishElement(expression);
}
@@ -80,6 +80,11 @@ public class StandardInstructionVisitor extends InstructionVisitor {
if (!(psi instanceof PsiField) || !psi.hasModifierProperty(PsiModifier.VOLATILE)) {
memState.setVarValue(var, dfaSource);
}
if (var.getInherentNullability() == Nullness.NULLABLE && !memState.isNotNull(dfaSource) && instruction.isVariableInitializer()) {
DfaMemoryStateImpl stateImpl = (DfaMemoryStateImpl)memState;
stateImpl.setVariableState(var, stateImpl.getVariableState(var).withNullability(Nullness.NULLABLE));
}
} else if (dfaDest instanceof DfaTypeValue && ((DfaTypeValue)dfaDest).isNotNull()) {
checkNotNullable(memState, dfaSource, NullabilityProblem.assigningToNotNull, instruction.getRExpression());
}
@@ -30,6 +30,7 @@ import com.intellij.codeInspection.dataFlow.DfaMemoryState;
import com.intellij.codeInspection.dataFlow.InstructionVisitor;
import com.intellij.codeInspection.dataFlow.value.DfaValue;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiVariable;
import org.jetbrains.annotations.Nullable;
public class AssignInstruction extends Instruction {
@@ -46,10 +47,15 @@ public class AssignInstruction extends Instruction {
return visitor.visitAssign(this, runner, stateBefore);
}
@Nullable
public PsiExpression getRExpression() {
return myRExpression;
}
public boolean isVariableInitializer() {
return myRExpression != null && myRExpression.getParent() instanceof PsiVariable;
}
@Nullable
public DfaValue getAssignedValue() {
return myAssignedValue;
@@ -0,0 +1,15 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
abstract class IDEATest {
abstract Object someMethod(@NotNull Object someParam);
abstract Object someObject();
public void testIDEA() {
@Nullable Object obj2 = someObject();
someMethod(<warning descr="Argument 'obj2' might be null">obj2</warning>);
}
}
@@ -198,6 +198,8 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase {
public void testNullCheckDoesntAffectUncheckedCast() { doTest(); }
public void testThrowNull() { doTest(); }
public void testExplicitlyNullableLocalVar() { doTest(); }
public void testTryWithResourcesNullability() { doTest(); }
public void testTryWithResourcesInstanceOf() { doTest(); }
public void testOmnipresentExceptions() { doTest(); }