Assert.fail() in catch still executes finally (IDEA-81084)

This commit is contained in:
peter
2012-10-29 12:57:22 +01:00
parent 47816dcc21
commit b2ca4d8a33
3 changed files with 37 additions and 3 deletions
@@ -500,12 +500,16 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
addInstruction(new CheckReturnValueInstruction(statement));
}
returnCheckingFinally();
finishElement(statement);
}
private void returnCheckingFinally() {
int finallyOffset = getFinallyOffset();
if (finallyOffset != NOT_FOUND) {
addInstruction(new GosubInstruction(finallyOffset));
}
addInstruction(new ReturnInstruction());
finishElement(statement);
}
@Override public void visitSwitchLabelStatement(PsiSwitchLabelStatement statement) {
@@ -1254,7 +1258,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
boolean testng = "org.testng.Assert".equals(className);
if ("fail".equals(methodName)) {
pushParameters(params, false, !testng);
addInstruction(new ReturnInstruction());
returnCheckingFinally();
return true;
}
else if ("assertTrue".equals(methodName)) {
@@ -1297,7 +1301,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
param.accept(this);
addInstruction(new PopInstruction());
}
addInstruction(new ReturnInstruction());
returnCheckingFinally();
return true;
}
else if ("assertTrue".equals(methodName)) {
@@ -0,0 +1,24 @@
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
class Test {
public static void foo() {
String result = null;
try {
result = createString();
}
catch (Exception e) {
Assert.fail();
}
finally {
if (result == null) {
System.out.println("Analysis failed!");
}
}
}
private static @NotNull String createString() {
throw new NullPointerException();
}
}
@@ -91,6 +91,12 @@ public class DataFlowInspectionFixtureTest extends JavaCodeInsightFixtureTestCas
public void testReturningNullFromVoidMethod() throws Throwable { doTest(); }
public void testCatchRuntimeException() throws Throwable { doTest(); }
public void testAssertFailInCatch() throws Throwable {
myFixture.addClass("package org.junit; public class Assert { public static void fail() {}}");
doTest();
}
public void testPreserveNullableOnUncheckedCast() throws Throwable { doTest(); }
}