From 12553cb3de5333cb5472c7071c0c6ef555419b1b Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 22 Sep 2014 12:38:11 +0200 Subject: [PATCH] IDEA-130111 "Constant conditions & exceptions" inspection: false positive for possible NPE --- .../dataFlow/DfaMemoryStateImpl.java | 17 ++++++-------- .../dataFlow/fixture/EnumConstantNotNull.java | 23 +++++++++++++++++++ .../DataFlowInspectionTest.java | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/EnumConstantNotNull.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java index 6790be21f52a..e1ec00c16c91 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaMemoryStateImpl.java @@ -464,8 +464,6 @@ public class DfaMemoryStateImpl implements DfaMemoryState { @Override public boolean isNull(DfaValue dfaValue) { - if (dfaValue instanceof DfaTypeValue && ((DfaTypeValue)dfaValue).isNotNull()) return false; - if (dfaValue instanceof DfaConstValue) return ((DfaConstValue)dfaValue).getValue() == null; if (dfaValue instanceof DfaVariableValue) { @@ -478,14 +476,13 @@ public class DfaMemoryStateImpl implements DfaMemoryState { @Override public boolean isNotNull(DfaValue dfaVar) { - if (dfaVar instanceof DfaVariableValue && getVariableState((DfaVariableValue)dfaVar).isNotNull()) { - return true; - } - if (dfaVar instanceof DfaConstValue && ((DfaConstValue)dfaVar).getValue() != null) { - return true; - } - if (dfaVar instanceof DfaTypeValue && ((DfaTypeValue)dfaVar).isNotNull()) { - return true; + if (dfaVar instanceof DfaConstValue) return ((DfaConstValue)dfaVar).getValue() != null; + if (dfaVar instanceof DfaTypeValue) return ((DfaTypeValue)dfaVar).isNotNull(); + if (dfaVar instanceof DfaVariableValue) { + if (getVariableState((DfaVariableValue)dfaVar).isNotNull()) return true; + + DfaConstValue constantValue = getConstantValue((DfaVariableValue)dfaVar); + if (constantValue != null && constantValue.getValue() != null) return true; } DfaConstValue dfaNull = myFactory.getConstFactory().getNull(); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/EnumConstantNotNull.java b/java/java-tests/testData/inspection/dataFlow/fixture/EnumConstantNotNull.java new file mode 100644 index 000000000000..c9b2f78b2a96 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/EnumConstantNotNull.java @@ -0,0 +1,23 @@ +import org.jetbrains.annotations.Nullable; + +class FooWithComments { + void unimportantMethod() { + AnEnum ae = nullableGetter(); + if (ae != null) { + if (ae == AnEnum.ENUM_VALUE) { + anotherMethod(ae.name()); // IDEA warns that ae.name() could cause NPE + } else { + // do something else + } + } + } + + private native void anotherMethod(String name); + + @Nullable + private native AnEnum nullableGetter(); + + enum AnEnum { + ENUM_VALUE, FOO2 + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index 92465cf858bc..85478517138d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -91,6 +91,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testVisitFinallyOnce() throws Throwable { doTest(); } public void testNotEqualsDoesntImplyNotNullity() throws Throwable { doTest(); } public void testEqualsEnumConstant() throws Throwable { doTest(); } + public void testEnumConstantNotNull() throws Throwable { doTest(); } public void testEqualsConstant() throws Throwable { doTest(); } public void testFinalLoopVariableInstanceof() throws Throwable { doTest(); } public void testGreaterIsNotEquals() throws Throwable { doTest(); }