FP on try-with-resources with @MustBeClosed

  • SonarQube Enterprise Edition, Version 7.6 (build 21501)

I have the following code:
import com.google.errorprone.annotations.MustBeClosed;

        @MustBeClosed
        protected Connection createConnection() throws SQLException {
             // my method does a couple of other things here so it's not totally pointless
            return dataSource.getConnection();
        }

SonarQube tells me “Use try-with-resources or close this “Connection” in a “finally” clause”, and declares that this is a “blocker” level bug. I’m not using try-with-resources here because this is a utility method that various other methods will call, and they will all use try-with-resources. If they don’t, then error-prone will complain since the method is annotated with @MustBeClosed.

So I believe that this is a false positive.

Hi,

This is indeed a false positive but we have absolutely no plan to support error-prone annotations at the moment.
What is the version of sonarjava on your server ? I can’t seem to reproduce the issue with latest version of sonar-java.

The dataSource field is a javax.sql.DataSource, fwiw.

The sonarjava is 5.10.1 (build 16922).