Hello all,
I've got this little sample of code :
FileInputStream fis = null;
FileOutputStream fos = null;
FileOutputStream fos2 = null;
try {
fis = new FileInputStream(new File(""));
fos = new FileOutputStream(new File(""));
fos2 = new FileOutputStream(new File(""));
// ...
} catch (IOException e) {
// log
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// log
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// log
}
}
if (fos2 != null) {
try {
fos2.close();
} catch (IOException e) {
// log
}
}
}
Sonar says the two last resources (in red, above) should be closed, but they are closed in the finally block exactly like the first resource, which does not return any error.
And if I switch, for example, the fos closure and the fis closure, the following lines are highlighted, just like it just cares about the first "try/catch" (or "if") block in the finally block :
FileInputStream fis = null;
FileOutputStream fos = null;
FileOutputStream fos2 = null;
try {
fis = new FileInputStream(new File(""));
fos = new FileOutputStream(new File(""));
fos2 = new FileOutputStream(new File(""));
// ...
} catch (IOException e) {
// log
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// log
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// log
}
}
if (fos2 != null) {
try {
fos2.close();
} catch (IOException e) {
// log
}
}
}
I use the last stable SonarLint version, and SonarQube 6.1.
Best regards,
Adrien