IDEA-97313 @Nullable inspection does not warn on dereferencing @Nullable variable declared in foreach loop

This commit is contained in:
peter
2012-12-19 19:36:33 +01:00
parent 4341bc7222
commit e9b67cad4c
3 changed files with 40 additions and 13 deletions
@@ -368,11 +368,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
int offset = myCurrentFlow.getInstructionCount();
DfaVariableValue dfaVariable = myFactory.getVarFactory().createVariableValue(parameter, false);
addInstruction(new PushInstruction(dfaVariable, null));
pushUnknown();
addInstruction(new AssignInstruction(null));
addInstruction(new PopInstruction());
addInstruction(new FlushVariableInstruction(dfaVariable));
pushUnknown();
addInstruction(new ConditionalGotoInstruction(getEndOffset(statement), true, null));
@@ -0,0 +1,20 @@
import foo.Nullable;
import java.util.ArrayList;
import java.util.List;
public class Foo {
void foo1(List<Integer> list) {
for (@Nullable Integer i : list) {
System.out.println(<warning descr="Method invocation 'i.intValue()' may produce 'java.lang.NullPointerException'">i.intValue()</warning>);
}
}
void foo2(List<@Nullable Integer> list) {
for (@Nullable Integer i : list) {
System.out.println(<warning descr="Method invocation 'i.intValue()' may produce 'java.lang.NullPointerException'">i.intValue()</warning>);
}
}
}
@@ -18,6 +18,8 @@ package com.intellij.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.NullableNotNullManager;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
@@ -106,18 +108,23 @@ public class DataFlowInspectionFixtureTest extends JavaCodeInsightFixtureTestCas
public void testEqualsImpliesNotNull() throws Throwable { doTest(); }
public void testAnnotatedTypeParameters() throws Throwable {
setupCustomAnnotations();
doTest();
}
private void setupCustomAnnotations() {
myFixture.addClass("package foo; public @interface Nullable {}");
myFixture.addClass("package foo; public @interface NotNull {}");
NullableNotNullManager nnnManager = NullableNotNullManager.getInstance(getProject());
final NullableNotNullManager nnnManager = NullableNotNullManager.getInstance(getProject());
nnnManager.setNotNulls("foo.NotNull");
nnnManager.setNullables("foo.Nullable");
try {
doTest();
}
finally {
nnnManager.setNotNulls();
nnnManager.setNullables();
}
Disposer.register(myTestRootDisposable, new Disposable() {
@Override
public void dispose() {
nnnManager.setNotNulls();
nnnManager.setNullables();
}
});
}
public void testSkipAssertions() {
@@ -140,5 +147,9 @@ public class DataFlowInspectionFixtureTest extends JavaCodeInsightFixtureTestCas
public void testMethodCallFlushesField() { doTest(); }
public void testUnknownFloatMayBeNaN() { doTest(); }
public void testLastConstantConditionInAnd() { doTest(); }
public void testNullableForeachVariable() {
setupCustomAnnotations();
doTest();
}
}