mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
null argument checker: ignore parameters if method throws exception anyway (IDEA-177823)
This commit is contained in:
+12
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user