java:S1143 false positive for return inside lambda in finally block

Product: SonarQube Community 26.4.0.121862
sonar-java version: sonar-java 8.28.0.43176
sonar-java SE version: sonar-java-symbolic-execution 8.16.3.1589
Java source level: 21 (javac 21, source/target 17)

Rule

java:S1143 — “Return” statements should not occur in “finally” blocks

Description

S1143 incorrectly reports a return inside a lambda expression as a return from the enclosing finally block. In this example, the finally block itself contains no method-level return; the return only exits the lambda body and cannot suppress exceptions from the try block. Rewriting the lambda as an equivalent anonymous inner class removes the warning, showing inconsistent handling of lambdas and anonymous classes.

Reproducer

public void overwriteLastBqi(long bqId, long bqiId) {
    try {
        performWork();
    } catch (Exception e) {
        handleError(e);
    } finally {
        // BEFORE — S1143 fires on the `return` inside the lambda
        Collection<ServiceExecutionEntry> untracked =
            serviceExecutionTracker.untrackMatchingEntries(e -> {
                ServiceExecutionIdentifiers ids = e.getIdentifiers();
                Long execBqiId = (ids == null) ? null : ids.getBqiId();
                return Objects.equals(bqiId, execBqiId); // FP: nested lambda return
            });
​
        // AFTER — semantically identical, S1143 does NOT fire
        Collection<ServiceExecutionEntry> untracked2 =
            serviceExecutionTracker.untrackMatchingEntries(
                new Predicate<ServiceExecutionEntry>() {
                    @Override
                    public boolean test(ServiceExecutionEntry e) {
                        ServiceExecutionIdentifiers ids = e.getIdentifiers();
                        Long execBqiId = (ids == null) ? null : ids.getBqiId();
                        return Objects.equals(bqiId, execBqiId); 
                    }
                });
​
        untracked.forEach(e -> System.out.println(e));
    }
}

Hi @Emilyaxe,

We have confirmed the false-positive and created a ticket to track the fix: SONARJAVA-6326.

Your ML research tool for compiler and systems optimization is producing some valuable input. Thank you for the detailed report, it helps!

Have a great weekend! (I’m hoping your AI bot gets weekends off too… or at least takes a break from finding our bugs until Monday :wink: )

Alban

Hi Alban,

Thank you for the quick response and for confirming the issue.

Yes, I have been using some ML research tools to help find inconsistencies in static analysis tools. I’ll also try to control the pace of my reports. If it ever becomes noisy or inconvenient on your side, please feel free to let me know.

When you have time, could you also take a look at these two reports?

Thanks again!

Best,