From 4d0c8ab748513d87e1002f763003d4884716e8eb Mon Sep 17 00:00:00 2001 From: peter Date: Sat, 13 Sep 2014 12:34:09 +0200 Subject: [PATCH] IDEA-127352 no warning when @NotNull annotated method returns null --- .../codeInspection/dataFlow/value/DfaConstValue.java | 4 ++++ .../dataFlow/fixture/ReturningNullConstant.java | 11 +++++++++++ .../codeInspection/DataFlowInspectionTest.java | 1 + 3 files changed, 16 insertions(+) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/ReturningNullConstant.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaConstValue.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaConstValue.java index 5c8d7ca276d5..49028dd66e7b 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaConstValue.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaConstValue.java @@ -70,6 +70,10 @@ public class DfaConstValue extends DfaValue { DfaConstValue unboxed = createFromValue(boo, PsiType.BOOLEAN, variable); return myFactory.getBoxedFactory().createBoxed(unboxed); } + PsiExpression initializer = variable.getInitializer(); + if (initializer instanceof PsiLiteralExpression && initializer.textMatches(PsiKeyword.NULL)) { + return dfaNull; + } return null; } return createFromValue(value, type, variable); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReturningNullConstant.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReturningNullConstant.java new file mode 100644 index 000000000000..4f9cfddc9f54 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/ReturningNullConstant.java @@ -0,0 +1,11 @@ +import org.jetbrains.annotations.NotNull; + +class BrokenAlignment { + + private static final String DEFAULT = null; + + @NotNull + public String getDefaultValue() { + return DEFAULT; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index 0acf071bbf4a..490ded30353c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -111,6 +111,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testSynchronizingOnNullable() throws Throwable { doTest(); } public void testSwitchOnNullable() { doTest(); } public void testReturningNullFromVoidMethod() throws Throwable { doTest(); } + public void testReturningNullConstant() { doTest(); } public void testCatchRuntimeException() throws Throwable { doTest(); } // IDEA-129331