diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractChecker.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractChecker.java index b527b8c51da7..7fad6fddd121 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractChecker.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractChecker.java @@ -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 myViolations = ContainerUtil.newHashSet(); private final Set myNonViolations = ContainerUtil.newHashSet(); private final Set myFailures = ContainerUtil.newHashSet(); + private final Set 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"); diff --git a/java/java-tests/testData/inspection/dataFlow/contractCheck/NestedCallsMayThrow.java b/java/java-tests/testData/inspection/dataFlow/contractCheck/NestedCallsMayThrow.java new file mode 100644 index 000000000000..073e3fab1bec --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/contractCheck/NestedCallsMayThrow.java @@ -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(); + +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ContractCheckTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/ContractCheckTest.java index 84ffb5244d96..c2cddc127835 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ContractCheckTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ContractCheckTest.java @@ -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(); } }