You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] UselessPureMethodCall: False positive for Stream.forEach #6781
PMD 7.25.0 reports some usages of Stream.forEach when forEach is used to add elements to an existing collection declared as var. The same code works ok when that collection type is declared explicitly.
The only difference between methods in the sample code is the declaration of the result variable.
PMD 7.24.0 works fine.
Code Sample demonstrating the issue:
importjava.util.ArrayList;
importjava.util.List;
importjava.util.stream.Collectors;
publicclassUselessPureMethodCallStreamForEach {
List<Object> ok(List<Object> source) {
ArrayList<Object> result = source.stream()
.collect(Collectors.toCollection(ArrayList::new));
source.stream()
.forEach(result::add);
returnresult;
}
List<Object> triggersUselessPureMethodCall(List<Object> source) {
varresult = source.stream()
.collect(Collectors.toCollection(ArrayList::new));
// the line below triggers// UselessPureMethodCallStreamForEach:25 Rule:UselessPureMethodCall Priority:3 Do not call pure method forEach if the result is not used..source.stream()
.forEach(result::add);
returnresult;
}
}
Expected outcome:
PMD should not report UselessPureMethodCall for this code.
A side-effect of the changes to InvocationMatcher in #6712.
In JavaRuleUtil.isKnownPure(), KNOWN_PURE_METHODS.anyMatch(call) now returns true (since the argument isn't resolved) and !call.getMethodType().getReturnType().isVoid() also returns true (also because the argument isn't resolved). @zbynek I don't think that was intended?
changed the title [-][java] UselessPureMethodCall: False-Positive for Stream.forEach[/-][+][java] UselessPureMethodCall: False positive for Stream.forEach[/+]on Jun 18, 2026
Activity
UncleOwen commentedon Jun 15, 2026
A side-effect of the changes to
InvocationMatcherin #6712.In JavaRuleUtil.isKnownPure(),
KNOWN_PURE_METHODS.anyMatch(call)now returns true (since the argument isn't resolved) and!call.getMethodType().getReturnType().isVoid()also returns true (also because the argument isn't resolved). @zbynek I don't think that was intended?[-][java] UselessPureMethodCall: False-Positive for Stream.forEach[/-][+][java] UselessPureMethodCall: False positive for Stream.forEach[/+][doc] Update release notes (#6781, #6797)
[java] Fix pmd#6781: False positive in UselessPureMethodCall with unr…