[java] CloseResource: fix false positive with close on not closeable

This commit is contained in:
Andreas Dangel committed 2020-08-21 09:15:30 +02:00
1 parent 0e743cc542
commit 36f61f44aa
2 files changed
+20 -1

No files matched your search

@@ -623,7 +623,7 @@ public class CloseResourceRule extends AbstractJavaRule {
@Override
public Object visit(ASTPrimaryPrefix prefix, Object data) {
ASTName methodCall = prefix.getFirstChildOfType(ASTName.class);
if (methodCall != null) {
if (methodCall != null && isNodeInstanceOfResourceType(methodCall)) {
String closedVar = getVariableClosedByMethodCall(methodCall);
if (closedVar != null && isNotInFinallyBlock(prefix) && !reportedVarNames.contains(closedVar)) {
String violationMsg = closeInFinallyBlockMessageForVar(closedVar);
@@ -1363,6 +1363,25 @@ public class Foo {
int d = ois.read();
}
}
}
]]></code>
</test-code>
<test-code>
<description>false positive with close on not closeable</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.*;
import com.google.common.io.Closeables;
import com.google.common.io.Flushables;
public class Foo {
private static void flushAndCloseOutStream(OutputStream stream) throws IOException {
if (stream != null) {
Flushables.flush(stream, false);
}
Closeables.close(stream, false);
}
}
]]></code>
</test-code>