[Java:S3958] Falsely reports unused stream pipeline

Version used: SonarCloud
Java compiler source and target versions: 14

Simplified method with which the issue should be reproduced:

    public Stream<Double> method() {
        Stream<Double> streamOfNumbers = Stream.of(1d, 2d, 3d);
        return Optional.of(new Random().nextDouble())
                .filter(randomNumber -> randomNumber < 0.99)
                .map(randomNumber -> streamOfNumbers.map(number -> number + randomNumber))

In both cases we are returning the same stream, except in once case it gets an extra .map intermediate operation.

Actual method and FP:

Hey @grimsa,

Thanks a lot for the feedback and reproducer. I created the following ticket to handle it: SONARJAVA-3604

Note that this rule is based on our internal symbolic execution engine, which shows some limitations when exploring stream and lambdas. I scheduled the ticket for our next Java Analyzer release, but I can not guarantee that we will manage to fix it by then.


1 Like

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