IDEA-187645 NPE warning is not issued when "null -> null" method is called with Nullable argument

This commit is contained in:
Tagir Valeev
2018-03-03 15:02:58 +07:00
parent 5ce39944b6
commit 48848a8a7b
2 changed files with 32 additions and 3 deletions
@@ -16,6 +16,7 @@
package com.intellij.codeInspection.dataFlow.value;
import com.intellij.codeInspection.dataFlow.DfaFactType;
import com.intellij.openapi.util.Trinity;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.tree.IElementType;
@@ -189,14 +190,22 @@ public class DfaRelationValue extends DfaValue {
return createCanonicalRelation(dfaLeft, relationType, dfaRight);
}
if (dfaLeft instanceof DfaFactMapValue && dfaRight instanceof DfaConstValue) {
return createCanonicalRelation(DfaUnknownValue.getInstance(), relationType, dfaRight);
return createConstBasedRelation((DfaFactMapValue)dfaLeft, relationType, (DfaConstValue)dfaRight);
}
else if (dfaRight instanceof DfaFactMapValue && dfaLeft instanceof DfaConstValue) {
return createCanonicalRelation(DfaUnknownValue.getInstance(), relationType, dfaLeft);
return createConstBasedRelation((DfaFactMapValue)dfaRight, relationType, (DfaConstValue)dfaLeft);
}
return null;
}
@NotNull
private DfaRelationValue createConstBasedRelation(DfaFactMapValue dfaLeft, RelationType relationType, DfaConstValue dfaRight) {
if (dfaRight.getValue() == null && Boolean.TRUE.equals(dfaLeft.get(DfaFactType.CAN_BE_NULL))) {
return createCanonicalRelation(myFactory.getFactValue(DfaFactType.CAN_BE_NULL, Boolean.TRUE), relationType, dfaRight);
}
return createCanonicalRelation(DfaUnknownValue.getInstance(), relationType, dfaRight);
}
@NotNull
private DfaRelationValue createCanonicalRelation(@NotNull final DfaValue dfaLeft,
@NotNull RelationType relationType,
@@ -2,6 +2,26 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Test2 {
interface Project {}
interface Sdk {}
interface Version {}
@NotNull
native static Test2 getInstance(Project project);
@Nullable
native Sdk getProjectSdk();
@Contract("null -> null")
native static Version getVersion(@Nullable Sdk sdk);
static void test(Project project) {
Version version = getVersion(getInstance(project).getProjectSdk());
System.out.println(version.<warning descr="Method invocation 'hashCode' may produce 'java.lang.NullPointerException'">hashCode</warning>());
}
}
class Foo {
public void main(@NotNull Object nn) {
@@ -58,4 +78,4 @@ class Test {
String test() {
return convert(getName());
}
}
}