Hello,
We are setting up sonar in our company and we are stuck on a specific rule for Java.
This rule is Use try-with-resources or close this “FileReader” in a “finally” clause.
In my first example, Sonarqube and Sonarlint show me a bug on “new FileReader(file)”
sample 1 : Noncompliant
public void test(final File file) throws IOException {
FileReader reader = new FileReader(file);
char c = (char) reader.read();
if (c == '"') {
// do something
}
reader.close();
}
However in my second example, Sonarqube and Sonarlint doesn’t show me any error
sample 2 : Compliant
public void test(final File file) throws IOException {
FileReader reader = new FileReader(file);
subTest(reader);
}
public void subTest(final FileReader reader) throws IOException {
char c = (char) reader.read();
if (c == '"') {
// do something
}
reader.close();
}
Is there something i’m actually missing ? Or, does it exist any other rule which corresponds to ressource closure on several levels ?
Thank you in advance for your answer.
Paul.