IDEA-196563 Wrong “Contract issue” inspection stating a method always fails

This commit is contained in:
Tagir Valeev
2018-08-02 03:00:49 +07:00
parent 60426c60b3
commit 635ceeee71
3 changed files with 27 additions and 9 deletions
@@ -70,10 +70,18 @@ class ContractChecker extends DataFlowRunner {
@Override
protected DfaInstructionState[] acceptInstruction(@NotNull InstructionVisitor visitor, @NotNull DfaInstructionState instructionState) {
DfaMemoryState memState = instructionState.getMemoryState();
Instruction instruction = instructionState.getInstruction();
if (instruction instanceof ReturnInstruction) {
if (((ReturnInstruction)instruction).isViaException()) {
ContainerUtil.addIfNotNull(myFailures, ((ReturnInstruction)instruction).getAnchor());
} else {
myMayReturnNormally = true;
}
}
if (memState.isEphemeral()) {
return super.acceptInstruction(visitor, instructionState);
}
Instruction instruction = instructionState.getInstruction();
if (instruction instanceof CheckReturnValueInstruction) {
PsiElement anchor = ((CheckReturnValueInstruction)instruction).getReturn();
DfaValue retValue = memState.pop();
@@ -86,14 +94,6 @@ class ContractChecker extends DataFlowRunner {
}
if (instruction instanceof ReturnInstruction) {
if (((ReturnInstruction)instruction).isViaException()) {
ContainerUtil.addIfNotNull(myFailures, ((ReturnInstruction)instruction).getAnchor());
} else {
myMayReturnNormally = true;
}
}
if (instruction instanceof MethodCallInstruction &&
((MethodCallInstruction)instruction).getMethodType() == MethodCallInstruction.MethodType.REGULAR_METHOD_CALL) {
if (myContract.getReturnValue().isFail()) {
@@ -0,0 +1,17 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
class Foo {
// IDEA-196563
@Nullable
@Contract(pure = true, value = "null, _, true -> fail; _, _, true -> !null")
private static Object getParam(@Nullable Map<String, Object> params, @NotNull String paramName, boolean required) {
final Object value = params == null ? null : params.get(paramName);
if (value == null && required) {
throw new IllegalArgumentException("Parameter not found");
}
return value;
}
}
@@ -54,4 +54,5 @@ public class ContractCheckTest extends LightCodeInsightFixtureTestCase {
public void testMutationSignatureProblems() { doTest(); }
public void testNewThisParam() { doTest(); }
public void testConditionsConflict() { doTest(); }
public void testWrongFailSuggestion() { doTest(); }
}