S2222: False-positive for Lock#tryLock

  • Sonar version 7.9.1 (build 27448)
  • error observed (wrap logs/code around triple quote ``` for proper formatting)
  • Paste code and run analysis with default profile for java
public static void main(String[] args) throws Exception {
        Callable<Void> job = () -> {
            return null;
        };
        executeWithLock(job, new ReentrantReadWriteLock().readLock(), "");
    }
    private static <V> V executeWithLock(Callable<V> operation, Lock lock, String errMsg) throws Exception {
        if (lock.tryLock()) {
            try {
                return operation.call();
            } finally {
                lock.unlock();
            }
        } else {
            throw new ConflictingOperationInProgressException(errMsg);
        }
    }

In fact, here is used a common pattern for lock, which is described in tryLock() javadoc:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html#tryLock()

Hello,

I know it’s an old topic, but I just stumble into this issue and realized that it’s still there in the current version.

I created a ticket to track it (SONARJAVA-3329).

Best,
Quentin

Thank you, Quentin