mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-132665 (IOResource opened but not safely closed: allow insignificant statements between opening a resource and 'try' keyword)
This commit is contained in:
+24
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user