False negative java:S3626: a redundant return inside a lambda body is not reported

Product/version: SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177), sonar-scanner 8.1.0.6389

Language: Java

Rule: java:S3626

Issue:

S3626 reports the return; at the end of run(int x) { System.out.println(x); return; }. The same two statements in a lambda that’s run right away aren’t reported, but in an anonymous Runnable they are (control/WSAnonRedundantReturn.java).

RedundantJumpCheck registers for Tree.Kind.METHOD and CONSTRUCTOR and walks methodTree.cfg(), which doesn’t include lambda bodies (a lambda has its own cfg()).

Reproducer:

seed/WSRedundantReturn.java:

public class WSRedundantReturn {
    public void run(int x) {
        System.out.println(x);
        return;
    }
}

variant/WSRedundantReturn.java:

public class WSRedundantReturn {
    public void run(int x) {
        Runnable r = () -> {
            System.out.println(x);
            return;
        };
        r.run();
    }
}

control/WSAnonRedundantReturn.java:

public class WSAnonRedundantReturn {
    public void run(int x) {
        Runnable r = new Runnable() {
            public void run() {
                System.out.println(x);
                return;
            }
        };
        r.run();
    }
}

Expected behavior:

S3626 (“Remove this redundant jump.”) on the return; inside the lambda in variant/WSRedundantReturn.java, as in seed/.

How to reproduce:

# one project per file
sonar-scanner -Dsonar.projectKey=<key> -Dsonar.sources=. \
    -Dsonar.java.binaries=. -Dsonar.scm.disabled=true

Output of the commands above:

### SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177), server reports 26.9.0.129388, default Sonar way profile  rule: S3626

--- SEED -- expected: reported (seed/)
    java:S1220             line -    Move this file to a named package.
    java:S106              line 3    Replace this use of System.out by a logger.
    java:S3626             line 4    Remove this redundant jump.  <== S3626
    [S3626: 1]

--- VARIANT -- expected: reported; actual: not reported (variant/)
    java:S1220             line -    Move this file to a named package.
    java:S106              line 4    Replace this use of System.out by a logger.
    [S3626: 0]

--- CONTROL -- the same rule on the control file, expected: reported (control/)
    java:S1220             line -    Move this file to a named package.
    java:S1604             line 3    Make this anonymous inner class a lambda (sonar.java.source
    java:S9357             line 3    Make this anonymous inner class a lambda (sonar.java.source
    java:S106              line 5    Replace this use of System.out by a logger.
    java:S3626             line 6    Remove this redundant jump.  <== S3626
    [S3626: 1]

Hi @MarkLee131!

Thank you for reporting this issue and providing a clear reproducer.

I have created a ticket to track it here: SONARJAVA-7280

Thanks again for your contribution!

Romain