Language: java
Rule: S2589
Product: SonarQube Community Edition Version 8.9.10 (build 61524)
Description:
- A variable is initialized as the first instruction in a try-block
- A non-null check for the variable is put on finally block
- Sonar reports that the field cannot never be null, and the check is gratuitous.
Code snippet reproducing the problem:
File file = null;
try {
file = new File("xxx");
...
} finally {
if (file != null) {
file.delete();
}
}
I think it is a false positive, because the constructor itself can throw Exception and the assignment to variable is never done. The check is in a finally block, so the field CAN be actually null.
Original screenshots for reference
