IDEA-132665 (IOResource opened but not safely closed: allow insignificant statements between opening a resource and 'try' keyword)

This commit is contained in:
Bas Leijdekkers
2014-11-12 12:29:20 +01:00
parent c0225f8050
commit cafdb6521f
2 changed files with 36 additions and 0 deletions
@@ -138,6 +138,9 @@ public abstract class ResourceInspection extends BaseInspection {
}
}
}
while (isInsignificant(nextStatement)) {
nextStatement = PsiTreeUtil.getNextSiblingOfType(nextStatement, PsiStatement.class);
}
while (nextStatement == null) {
statement = PsiTreeUtil.getParentOfType(statement, PsiStatement.class, true);
if (statement == null) {
@@ -160,6 +163,27 @@ public abstract class ResourceInspection extends BaseInspection {
return isResourceClose(nextStatement, variable);
}
private static boolean isInsignificant(PsiStatement statement) {
if (statement == null) {
return false;
}
final boolean[] result = {true};
statement.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitExpression(PsiExpression expression) {
super.visitExpression(expression);
result[0] = false;
}
@Override
public void visitElement(PsiElement element) {
super.visitElement(element);
if (!result[0]) stopWalking();
}
});
return result[0];
}
protected static boolean isResourceClosedInFinally(@NotNull PsiTryStatement tryStatement, @NotNull PsiVariable variable) {
final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
if (finallyBlock == null) {
@@ -178,4 +178,16 @@ public class IOResource {
InputStream in = new FileInputStream("");
escaper(in);
}
void insignificant() throws IOException {
InputStream in = new FileInputStream("file");
Object o;
{;;};
try {
o = in.read();
}
finally {
in.close();
}
}
}