IDEA-138859 (Inspection 'throw inside 'finally' block'' is too eager)

This commit is contained in:
Bas Leijdekkers
2015-04-07 17:25:50 +02:00
parent b942b9d6e8
commit afdbce7fa9
4 changed files with 115 additions and 43 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -319,6 +319,15 @@ public class ControlFlowUtils {
}
public static boolean isInFinallyBlock(@NotNull PsiElement element) {
final PsiType type;
if (element instanceof PsiThrowStatement) {
final PsiThrowStatement throwStatement = (PsiThrowStatement)element;
final PsiExpression exception = throwStatement.getException();
type = exception != null ? exception.getType() : null;
}
else {
type = null;
}
PsiElement currentElement = element;
while (true) {
final PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(currentElement, PsiTryStatement.class, true, PsiClass.class, PsiLambdaExpression.class);
@@ -326,17 +335,24 @@ public class ControlFlowUtils {
return false;
}
final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
if (finallyBlock != null) {
if (PsiTreeUtil.isAncestor(finallyBlock, currentElement, true)) {
final PsiMethod elementMethod = PsiTreeUtil.getParentOfType(currentElement, PsiMethod.class);
final PsiMethod finallyMethod = PsiTreeUtil.getParentOfType(finallyBlock, PsiMethod.class);
return elementMethod != null && elementMethod.equals(finallyMethod);
}
if (PsiTreeUtil.isAncestor(finallyBlock, currentElement, true)) {
return true;
}
if (type != null && isCaught(tryStatement, type)) {
return false;
}
currentElement = tryStatement;
}
}
public static boolean isCaught(PsiTryStatement tryStatement, PsiType exceptionType) {
for (PsiParameter parameter : tryStatement.getCatchBlockParameters()) {
final PsiType type = parameter.getType();
if (type.isAssignableFrom(exceptionType)) return true;
}
return false;
}
public static boolean isInCatchBlock(@NotNull PsiElement element) {
return PsiTreeUtil.getParentOfType(element, PsiCatchSection.class, true, PsiClass.class) != null;
}
@@ -0,0 +1,51 @@
package com.siyeh.igtest.errorhandling.throw_from_finally_block;
import java.io.FileInputStream;
import java.io.IOException;
public class ThrowFromFinallyBlock
{
public void foo() throws Exception
{
try
{
return;
}
finally
{
<warning descr="'throw' inside 'finally' block">throw</warning> new Exception();
}
}
public void bar() throws Exception
{
try
{
return;
}
finally
{
try
{
<warning descr="'throw' inside 'finally' block">throw</warning> new Exception();
}
finally
{
<warning descr="'throw' inside 'finally' block">throw</warning> new Exception();
}
}
}
public void safe() throws IOException {
try (FileInputStream in = new FileInputStream("name")) {
} catch (RuntimeException e) {
// ...
} finally {
try {
throw new NullPointerException();
} catch (RuntimeException e) {}
}
}
}
@@ -1,36 +0,0 @@
package com.siyeh.igtest.exceptionHandling;
public class ThrowFromFinallyBlockInspection
{
public void foo() throws Exception
{
try
{
return;
}
finally
{
throw new Exception();
}
}
public void bar() throws Exception
{
try
{
return;
}
finally
{
try
{
throw new Exception();
}
finally
{
throw new Exception();
}
}
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2015 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.
*/
/**
* (c) 2015 Silent Forest AB
* created: 07 April 2015
*/
package com.siyeh.ig.errorhandling;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class ThrowFromFinallyBlockInspectionTest extends LightInspectionTestCase {
public void testThrowFromFinallyBlock() {
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new ThrowFromFinallyBlockInspection();
}
}