FP using Lombok @Cleanup

  • SonarQube
  • SonarJava (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());
        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) {

Hi. Welcome to the SonarQube community! :grin:

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

1 Like

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.