null argument checker: ignore parameters if method throws exception anyway (IDEA-177823)

This commit is contained in:
Dmitry Batkovich
2017-08-29 17:27:00 +03:00
parent 9430eaa658
commit f7dd4dfaa4
3 changed files with 31 additions and 3 deletions
@@ -50,10 +50,12 @@ import java.util.Set;
class NullParameterConstraintChecker extends DataFlowRunner {
private final Set<PsiParameter> myPossiblyViolatedParameters;
private final Set<PsiParameter> myUsedParameters;
private final Set<PsiParameter> myParametersWithSuccessfulExecutionInNotNullState;
private NullParameterConstraintChecker(Collection<PsiParameter> parameters) {
super(false, true);
myPossiblyViolatedParameters = new THashSet<>(parameters);
myParametersWithSuccessfulExecutionInNotNullState = new THashSet<>();
myUsedParameters = new THashSet<>();
}
@@ -74,10 +76,14 @@ class NullParameterConstraintChecker extends DataFlowRunner {
}
if (nullableParameters.isEmpty()) return PsiParameter.EMPTY_ARRAY;
final NullParameterConstraintChecker checker = new NullParameterConstraintChecker(nullableParameters);
NullParameterConstraintChecker checker = new NullParameterConstraintChecker(nullableParameters);
checker.analyzeMethod(method.getBody(), new StandardInstructionVisitor());
return checker.myPossiblyViolatedParameters.stream().filter(checker.myUsedParameters::contains).toArray(PsiParameter[]::new);
return checker.myPossiblyViolatedParameters
.stream()
.filter(checker.myUsedParameters::contains)
.filter(checker.myParametersWithSuccessfulExecutionInNotNullState::contains)
.toArray(PsiParameter[]::new);
}
@NotNull
@@ -108,7 +114,10 @@ class NullParameterConstraintChecker extends DataFlowRunner {
DfaMemoryState memState = instructionState.getMemoryState();
for (PsiParameter parameter : myPossiblyViolatedParameters.toArray(new PsiParameter[myPossiblyViolatedParameters.size()])) {
final DfaVariableValue dfaVar = getFactory().getVarFactory().createVariableValue(parameter, false);
if (!memState.isNotNull(dfaVar)) {
if (memState.isNotNull(dfaVar)) {
myParametersWithSuccessfulExecutionInNotNullState.add(parameter);
}
else {
myPossiblyViolatedParameters.remove(parameter);
}
}
@@ -0,0 +1,15 @@
class Test {
private static void printAndThrow(String message, Throwable cause) {
if (message != null) {
System.err.println(message);
}
if (cause != null) {
cause.printStackTrace();
}
throw new IllegalStateException();
}
public static void main(String[] args) {
printAndThrow("test", <warning descr="Passing 'null' argument to non annotated parameter">null</warning>);
printAndThrow(<warning descr="Passing 'null' argument to non annotated parameter">null</warning>, new NullPointerException());
}
}
@@ -117,6 +117,10 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase {
doTest();
}
public void testNullArgumentIfMethodExecutionFailsAnyway() {
doTest();
}
public void testNullableArrayComponent() {
setupCustomAnnotations();
DataFlowInspection inspection = new DataFlowInspection();