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:S3958 — “Stream” operations should not be ignored
Description
S3958 handles stream::iterator and the equivalent lambda () -> stream.iterator() inconsistently. Both forms consume the stream through the enhanced for loop inside the callee, but SonarJava reports S3958 only for the lambda form and not for the method reference.
Reproducer
package demo;
import java.util.List;
import java.util.stream.Stream;
public class StreamConsumptionCheck {
// BEFORE — no S3958 raised
boolean barRef(List<Boolean> list) {
Stream<Boolean> filter = list.stream().filter(Boolean::booleanValue);
return foo(filter::iterator);
}
// AFTER — S3958 raised ("Refactor the code so this stream pipeline is used.")
boolean barLambda(List<Boolean> list) {
Stream<Boolean> filter = list.stream().filter(Boolean::booleanValue);
return foo(() -> filter.iterator());
}
boolean foo(Iterable<Boolean> iterable) {
int count = 0;
for (Boolean b : iterable) {
if (b) count++;
}
return count > 0;
}
}