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 {
        } else {
            throw new ConflictingOperationInProgressException(errMsg);

In fact, here is used a common pattern for lock, which is described in tryLock() javadoc:


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).


Thank you, Quentin