null argument checker: filter out unused parameters

This commit is contained in:
Dmitry Batkovich
2016-10-17 15:22:57 +03:00
parent 1b2dbea09f
commit 9858aaf705
3 changed files with 25 additions and 1 deletions
@@ -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<PsiParameter> myPossiblyViolatedParameters;
private final Set<PsiParameter> myUsedParameters;
private NullParameterConstraintChecker(Collection<PsiParameter> 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) {
@@ -0,0 +1,10 @@
class Test {
void m() {
throwAnException(<warning descr="Passing 'null' argument to non annotated parameter">null</warning>);
}
static void throwAnException(String arg) {
throw new RuntimeException();
}
}
@@ -405,4 +405,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testNullLiteralAndInferredMethodContract() {
doTest();
}
public void testNullLiteralArgumentDoesntReportedWhenMethodOnlyThrowAnException() { doTest(); }
}