Skip to content

[java] Fix #6517: UselessPureMethodCall: fix false negative for primitive streams#6524

Merged
adangel merged 2 commits into
pmd:mainfrom
leemeii:6517
May 7, 2026
Merged

[java] Fix #6517: UselessPureMethodCall: fix false negative for primitive streams#6524
adangel merged 2 commits into
pmd:mainfrom
leemeii:6517

Conversation

@leemeii

@leemeii leemeii commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • fix false negative in UselessPureMethodCall for discarded peek(...) calls on IntStream, LongStream, and DoubleStream
  • add regression test covering Stream + three primitive stream variants

Root cause

UselessPureMethodCall depends on JavaRuleUtil.isKnownPure, which uses a known-pure invocation matcher list.
That list contained java.util.stream.Stream#(*) but did not include primitive stream types, so peek(...) on IntStream / LongStream / DoubleStream was not classified as pure and therefore not reported when its result was ignored.

Fix

Add primitive stream types to KNOWN_PURE_METHODS:

  • java.util.stream.IntStream#(*)
  • java.util.stream.LongStream#(*)
  • java.util.stream.DoubleStream#(*)

Verification

  • ran mvn test -Dtest=UselessPureMethodCallTest in pmd-java
  • result: 23 tests, 0 failures

Fixes #6517

@zbynek

zbynek commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

The PR description is a bit misleading -- this checks for all non-void methods on IntStream / DoubleStream / LongStream, which is good, but not specific to peek.

Like in the other PRs, please rebase this to avoid unrelated changes.

…o known-pure methods - fix pmd#6517

Add primitive stream types (IntStream, LongStream, DoubleStream) to
KNOWN_PURE_METHODS so that any discarded non-void method call on these
types is reported, not just peek.

Previously only java.util.stream.Stream was listed; the three primitive
stream variants were missing, causing a false negative for peek and all
other non-void methods on those types.
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

@adangel adangel changed the title [java] UselessPureMethodCall: fix false negative for primitive stream peek (#6517) [java] Fix #6517: UselessPureMethodCall: fix false negative for primitive stream peek May 7, 2026
@adangel adangel changed the title [java] Fix #6517: UselessPureMethodCall: fix false negative for primitive stream peek [java] Fix #6517: UselessPureMethodCall: fix false negative for primitive stream May 7, 2026
@adangel adangel changed the title [java] Fix #6517: UselessPureMethodCall: fix false negative for primitive stream [java] Fix #6517: UselessPureMethodCall: fix false negative for primitive streams May 7, 2026
@adangel adangel added this to the 7.25.0 milestone May 7, 2026
@pmd-actions-helper

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 1 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-05-07 08:58:21+00:00 for 0da431c)

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adangel adangel merged commit 0da431c into pmd:main May 7, 2026
13 checks passed
adangel added a commit that referenced this pull request May 7, 2026
adangel added a commit that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UselessPureMethodCall: False negative for methods on IntStream/LongStream/DoubleStream

3 participants