data flow inspection: getters without explicit nullable annotation are nullable if their fields are nullable

This commit is contained in:
Dmitry Batkovich
2017-09-22 11:35:55 +03:00
parent 839d461ced
commit a01c6525b4
4 changed files with 46 additions and 0 deletions

View File

@@ -110,6 +110,13 @@ public class DfaPsiUtil {
return inferParameterNullability((PsiParameter)owner);
}
if (owner instanceof PsiMethod) {
PsiField field = PropertyUtil.getFieldOfGetter((PsiMethod)owner);
if (field != null && getElementNullability(resultType, field) == Nullness.NULLABLE) {
return Nullness.NULLABLE;
}
}
return Nullness.UNKNOWN;
}

View File

@@ -0,0 +1,17 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class A {
@Nullable
private final String xxxx = "";
public String getXxxx() {
return xxxx;
}
}
class B {
@NotNull
String x = <warning descr="Expression 'new A().getXxxx()' might evaluate to null but is assigned to a variable that is annotated with @NotNull">new A().getXxxx()</warning>;
}

View File

@@ -0,0 +1,18 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class A {
@Nullable
private final String xxxx = "";
@NotNull
public String getXxxx() {
return xxxx;
}
}
class B {
@NotNull
String x = new A().getXxxx();
}

View File

@@ -542,4 +542,8 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testInnerClass() { doTest(); }
public void testCovariantReturn() { doTest(); }
public void testArrayInitializerLength() { doTest(); }
public void testGetterOfNullableFieldIsNotAnnotated() { doTest(); }
public void testGetterOfNullableFieldIsNotNull() { doTest(); }
}