From 8ac5d7c7d3c4b26dc7e7093d169e3ec86eff7ca4 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 14 Aug 2017 15:40:16 +0200 Subject: [PATCH] IDEA-177404 Method that never returns get "returns non-null value" warning --- .../codeInspection/dataFlow/DataFlowInspectionBase.java | 9 +++++---- .../dataFlow/fixture/NullableMethodReturningNotNull.java | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java index 85c457eb2077..1e660900cab7 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java @@ -348,7 +348,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { reportConstantReferenceValues(holder, visitor, reportedAnchors); } - if (REPORT_NULLABLE_METHODS_RETURNING_NOT_NULL && visitor.isAlwaysReturnsNotNull()) { + if (REPORT_NULLABLE_METHODS_RETURNING_NOT_NULL && visitor.isAlwaysReturnsNotNull(runner.getInstructions())) { reportAlwaysReturnsNotNull(holder, scope); } } @@ -410,7 +410,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { PsiJavaCodeReferenceElement annoName = nullableAnno.getNameReferenceElement(); assert annoName != null; String msg = "@" + NullableStuffInspectionBase.getPresentableAnnoName(nullableAnno) + - " method '" + method.getName() + "' always return a non-null value"; + " method '" + method.getName() + "' always returns a non-null value"; holder.registerProblem(annoName, msg, new AddNotNullAnnotationFix(method)); } @@ -976,8 +976,9 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { return StreamEx.ofKeys(myFailingCalls, v -> v).map(MethodCallInstruction::getCallExpression).toList(); } - boolean isAlwaysReturnsNotNull() { - return myAlwaysReturnsNotNull; + boolean isAlwaysReturnsNotNull(Instruction[] instructions) { + return myAlwaysReturnsNotNull && + ContainerUtil.exists(instructions, i -> i instanceof ReturnInstruction && ((ReturnInstruction)i).getAnchor() instanceof PsiReturnStatement); } @Override diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/NullableMethodReturningNotNull.java b/java/java-tests/testData/inspection/dataFlow/fixture/NullableMethodReturningNotNull.java index 70a7444bf308..3f04edada161 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/NullableMethodReturningNotNull.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/NullableMethodReturningNotNull.java @@ -1,7 +1,7 @@ import org.jetbrains.annotations.*; final class Foo { - @Nullable Object foo(int param) { + @Nullable Object foo(int param) { return param == 1 ? new Object() { Object unrelated() { return null; @@ -12,4 +12,9 @@ final class Foo { @NotNull Foo bar() { return this; } + + @Nullable + Object throwing() { + throw new UnsupportedOperationException(); + } }