diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/NullParameterConstraintChecker.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/NullParameterConstraintChecker.java index 218656cfb91d..39ba4a0e1475 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/NullParameterConstraintChecker.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/NullParameterConstraintChecker.java @@ -18,6 +18,7 @@ package com.intellij.codeInspection.dataFlow; import com.intellij.codeInsight.NullableNotNullManager; import com.intellij.codeInspection.dataFlow.instructions.AssignInstruction; import com.intellij.codeInspection.dataFlow.instructions.Instruction; +import com.intellij.codeInspection.dataFlow.instructions.PushInstruction; import com.intellij.codeInspection.dataFlow.instructions.ReturnInstruction; import com.intellij.codeInspection.dataFlow.value.DfaValue; import com.intellij.codeInspection.dataFlow.value.DfaValueFactory; @@ -47,10 +48,12 @@ import java.util.Set; */ class NullParameterConstraintChecker extends DataFlowRunner { private final Set myPossiblyViolatedParameters; + private final Set myUsedParameters; private NullParameterConstraintChecker(Collection parameters, boolean isOnTheFly) { super(false, true, isOnTheFly); myPossiblyViolatedParameters = new THashSet<>(parameters); + myUsedParameters = new THashSet<>(); } @NotNull @@ -73,7 +76,7 @@ class NullParameterConstraintChecker extends DataFlowRunner { final NullParameterConstraintChecker checker = new NullParameterConstraintChecker(nullableParameters, true); checker.analyzeMethod(method.getBody(), new StandardInstructionVisitor()); - return checker.myPossiblyViolatedParameters.toArray(new PsiParameter[checker.myPossiblyViolatedParameters.size()]); + return checker.myPossiblyViolatedParameters.stream().filter(checker.myUsedParameters::contains).toArray(PsiParameter[]::new); } @NotNull @@ -81,6 +84,16 @@ class NullParameterConstraintChecker extends DataFlowRunner { protected DfaInstructionState[] acceptInstruction(@NotNull InstructionVisitor visitor, @NotNull DfaInstructionState instructionState) { Instruction instruction = instructionState.getInstruction(); + if (instruction instanceof PushInstruction) { + final DfaValue var = ((PushInstruction)instruction).getValue(); + if (var instanceof DfaVariableValue) { + final PsiModifierListOwner psiVar = ((DfaVariableValue)var).getPsiVariable(); + if (psiVar instanceof PsiParameter) { + myUsedParameters.add((PsiParameter)psiVar); + } + } + } + if (instruction instanceof AssignInstruction) { final DfaValue value = ((AssignInstruction)instruction).getAssignedValue(); if (value instanceof DfaVariableValue) { diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/NullLiteralArgumentDoesntReportedWhenMethodOnlyThrowAnException.java b/java/java-tests/testData/inspection/dataFlow/fixture/NullLiteralArgumentDoesntReportedWhenMethodOnlyThrowAnException.java new file mode 100644 index 000000000000..aff8882cc70a --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/NullLiteralArgumentDoesntReportedWhenMethodOnlyThrowAnException.java @@ -0,0 +1,10 @@ +class Test { + + void m() { + throwAnException(null); + } + + static void throwAnException(String arg) { + throw new RuntimeException(); + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index 8d3e4c50b620..eb2a56ad9d22 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -405,4 +405,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { public void testNullLiteralAndInferredMethodContract() { doTest(); } + public void testNullLiteralArgumentDoesntReportedWhenMethodOnlyThrowAnException() { doTest(); } }