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));
}
}