I am using SonarQube 6.7.4 and trying to understand why the rule “Resources should be closed” squid:S2095 is not working the way it is documented.
The documentation says - “Connections, streams, files, and other classes that implement the Closeable interface or its super-interface, AutoCloseable, needs to be closed after use. Further, that close call must be made in a finally block”.
I have left open InputStream, StringWriter and PrintWriter, wrote no finally block, but the rule does not report any violation.
Could you double-check that those types are not excluded from your sonarqube configuration ? as the rule can be parameterized, I want to make sure this is not due to a misconfiguration.
For your first example I believe it is not raised because the analyzer don’t consider that the resource has been open within this method.
For the printwriter, as the stringwriter is excluded (and thus not open) the behavior seems expected.