False positive on "Boolean expressions should not be gratuitous"

Language: java
Rule: S2589
Product: SonarQube Community Edition Version 8.9.10 (build 61524)


  1. A variable is initialized as the first instruction in a try-block
  2. A non-null check for the variable is put on finally block
  3. 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) {

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


Hey there.

Thanks for the feedback.

It looks like this issue is already reported here: [SONARJAVA-3169] - Jira

Yes, it seems very similar.
Thank you.