contract checker: relax fail result validation to accept every call as a potential hazard

This commit is contained in:
peter
2014-09-30 17:23:49 +02:00
parent 001feb5648
commit b0a7ab05aa
3 changed files with 41 additions and 2 deletions
@@ -17,6 +17,7 @@ package com.intellij.codeInspection.dataFlow;
import com.intellij.codeInspection.dataFlow.instructions.CheckReturnValueInstruction;
import com.intellij.codeInspection.dataFlow.instructions.Instruction;
import com.intellij.codeInspection.dataFlow.instructions.MethodCallInstruction;
import com.intellij.codeInspection.dataFlow.instructions.ReturnInstruction;
import com.intellij.codeInspection.dataFlow.value.DfaConstValue;
import com.intellij.codeInspection.dataFlow.value.DfaValue;
@@ -37,6 +38,7 @@ class ContractChecker extends DataFlowRunner {
private final Set<PsiElement> myViolations = ContainerUtil.newHashSet();
private final Set<PsiElement> myNonViolations = ContainerUtil.newHashSet();
private final Set<PsiElement> myFailures = ContainerUtil.newHashSet();
private final Set<PsiElement> myCalls = ContainerUtil.newHashSet();
ContractChecker(PsiMethod method, PsiCodeBlock body, MethodContract contract, final boolean onTheFly) {
super(body);
@@ -102,7 +104,12 @@ class ContractChecker extends DataFlowRunner {
}
}
return super.acceptInstruction(visitor, instructionState);
if (instruction instanceof MethodCallInstruction &&
((MethodCallInstruction)instruction).getMethodType() == MethodCallInstruction.MethodType.REGULAR_METHOD_CALL) {
ContainerUtil.addIfNotNull(myCalls, ((MethodCallInstruction)instruction).getCallExpression());
}
return super.acceptInstruction(visitor, instructionState);
}
@@ -118,7 +125,7 @@ class ContractChecker extends DataFlowRunner {
for (PsiElement element : myFailures) {
errors.put(element, "Contract clause '" + myContract + "' is violated: exception might be thrown instead of returning " + myContract.returnValue);
}
} else if (myFailures.isEmpty() && errors.isEmpty()) {
} else if (myFailures.isEmpty() && errors.isEmpty() && myCalls.isEmpty()) {
PsiIdentifier nameIdentifier = myMethod.getNameIdentifier();
errors.put(nameIdentifier != null ? nameIdentifier : myMethod,
"Contract clause '" + myContract + "' is violated: no exception is thrown");
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class Foo {
@Contract("true->fail")
void assertFalse(boolean fail) {
if (fail) {
failNatively();
}
}
native void failNatively();
}
@@ -37,6 +37,7 @@ public class ContractCheckTest extends LightCodeInsightFixtureTestCase {
public void testEqualsUnknownValue() { doTest(); }
public void testMissingFail() { doTest(); }
public void testCheckSuperContract() { doTest(); }
public void testNestedCallsMayThrow() { doTest(); }
public void testSignatureIssues() { doTest(); }
}