FP using Lombok @Cleanup

java
(Paul Cowan) #1
  • SonarQube 7.7.0.23042
  • SonarJava 5.11.0.17289 (java)

The Lombok annotation @Cleanup triggers 2 false positives: squid:S1854 and squid:S1481

import lombok.Cleanup;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class CleanupTest {
    private Map<String, ReentrantLock> lockCache = new ConcurrentHashMap<>();

    private ReentrantLock getLock(String name) {
        ReentrantLock lock = lockCache.computeIfAbsent(name, s -> new ReentrantLock());
        lock.lock();
        return lock;
    }

    public void testCleanupLombok() {
        @Cleanup("unlock") ReentrantLock lock = getLock("foo");
        System.out.println("Do something");
    }

    public void testCleanupDeLomboked() {
        ReentrantLock lock = getLock("foo");
        try {
            System.out.println("Do something");
        } finally {
            if (java.util.Collections.singletonList(lock).get(0) != null) {
                lock.unlock();
            }
        }
    }
}
(Scott) #2

Hi. Welcome to the SonarQube community! :grin:

Unfortunately, I think this fell in the same situation as:

1 Like
(Paul Cowan) #3

There’s a whole LombokFIlter class dedicated to filtering these type of issues. Not sure it can handle this also but seems quite similar. If I have time I’ll mod it and submit PR.

2 Likes