Product: SonarQube Community (self-hosted)
sonar-java version: sonar-java 8.28.0.43176 on SonarQube Community Build 26.4.0.121862
Java source level: 21 (javac 21, source/target 17)
Rule
java:S2095 — Resources should be closed
Description
S2095 stops reporting an unclosed InputStream once it is passed to a helper method, even though the helper neither closes nor takes ownership of the resource. The stream still leaks, but the analyzer appears to treat any method call as potential resource management.
Reproducer
// BEFORE — S2095 correctly reported on `new FileInputStream(path)`
package demo.before;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamLeak {
public static void process(String path) throws IOException {
InputStream in = new FileInputStream(path);
int firstByte = in.read();
System.out.println("First byte: " + firstByte);
}
}
// AFTER — S2095 no longer reported, but the stream is still leaked
package demo.after;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamLeak {
public static void process(String path) throws IOException {
InputStream in = new FileInputStream(path);
int firstByte = readFirstByte(in); // helper does not close `in`
System.out.println("First byte: " + firstByte);
}
private static int readFirstByte(InputStream in) throws IOException {
return in.read();
}
}