consider non-annotated method parameters also nullable in paranoid dfa mode (IDEA-129898)

This commit is contained in:
peter
2014-09-16 15:10:50 +02:00
parent 1f95c1fdaa
commit 7e061f5eef
3 changed files with 12 additions and 2 deletions
@@ -30,6 +30,7 @@ import com.intellij.codeInspection.dataFlow.DfaPsiUtil;
import com.intellij.codeInspection.dataFlow.Nullness;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Trinity;
import com.intellij.patterns.ElementPattern;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
@@ -40,8 +41,13 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import static com.intellij.patterns.PsiJavaPatterns.*;
public class DfaVariableValue extends DfaValue {
private static final ElementPattern<? extends PsiModifierListOwner> MEMBER_OR_METHOD_PARAMETER =
or(psiMember(), psiParameter().withSuperParent(2, psiMember()));
public static class Factory {
private final MultiMap<Trinity<Boolean,String,DfaVariableValue>,DfaVariableValue> myExistingVars = new MultiMap<Trinity<Boolean, String, DfaVariableValue>, DfaVariableValue>();
private final DfaValueFactory myFactory;
@@ -165,7 +171,7 @@ public class DfaVariableValue extends DfaValue {
return nullability;
}
Nullness defaultNullability = var instanceof PsiMember && myFactory.isUnknownMembersAreNullable() ? Nullness.NULLABLE : Nullness.UNKNOWN;
Nullness defaultNullability = myFactory.isUnknownMembersAreNullable() && MEMBER_OR_METHOD_PARAMETER.accepts(var) ? Nullness.NULLABLE : Nullness.UNKNOWN;
if (var instanceof PsiParameter && var.getParent() instanceof PsiForeachStatement) {
PsiExpression iteratedValue = ((PsiForeachStatement)var.getParent()).getIteratedValue();
@@ -117,7 +117,7 @@ public class DataFlowInspection extends DataFlowInspectionBase {
}
});
myTreatUnknownMembersAsNullable = new JCheckBox("Treat non-annotated methods and fields as @Nullable");
myTreatUnknownMembersAsNullable = new JCheckBox("Treat non-annotated members and parameters as @Nullable");
myTreatUnknownMembersAsNullable.setSelected(TREAT_UNKNOWN_MEMBERS_AS_NULLABLE);
myTreatUnknownMembersAsNullable.getModel().addChangeListener(new ChangeListener() {
@Override
@@ -7,6 +7,10 @@ class Test {
<warning descr="Method invocation 'o.hashCode()' may produce 'java.lang.NullPointerException'">o.hashCode()</warning>;
}
void parameter(Object o) {
<warning descr="Method invocation 'o.hashCode()' may produce 'java.lang.NullPointerException'">o.hashCode()</warning>;
}
void callUnknownMethod() {
<warning descr="Method invocation 'unknownObject().hashCode()' may produce 'java.lang.NullPointerException'">unknownObject().hashCode()</warning>;
}