Eclipse: java:S2184 return in lambda statement gives wrong casting hint because of different method return type

Environment
Eclipse IDE 2020-06 (4.16.0), build id 20200615-1200
SonarLint for Eclipse 5.2.1.18852
Windows 10 Pro 1903 build 18362.900

Problem

double compute() {
    long sum = List.of(1, 2, 3).stream().mapToLong(i -> {
        return i * i;
    }).sum();
    return sum;
}

SonarLint falsely warns in the lambda statement on the *

(java:S2184) Cast one of the operands of this multiplication operation to a “double”.

although the target type of the lambda is long and only the return type of the outer method is double. This bug may be related to this Eclipse bug because the return i * i; is falsely marked as exit point of the compute() method.

Hello @Madjosz, welcome to the community!

Thanks for the feedback and the clear description of the problem.

Indeed this is a wrong behavior of the rule. I created a ticket to track this issue: SONARJAVA-3479.

This bug may be related to this Eclipse bug

Thanks for pointing us to this bug, however, I don’t think it impacts us, at least, not for this rule.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.