Repository navigation
[java] Fix #6709: Fix false positive: LambdaCanBeMethodReference should not flag lambda… - #6828
Merged
UncleOwen merged 4 commits intoJul 6, 2026
Conversation
Contributor
|
Compared to main: (comment created at 2026-07-06 15:00:34+00:00 for b16526f) |
… with array constructor receiver Fixes pmd#6709
dweep-js
force-pushed
the
fix-lambdacanbemethodreference-array-constructor
branch
from
July 1, 2026 04:28
dd9c6cf to
075f077
Compare
Contributor
Author
|
@zbynek I have fixed it and done the changes please review again |
zbynek
approved these changes
Jul 1, 2026
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the PR
This PR fixes a false positive in the
LambdaCanBeMethodReferencerule where lambda expressions were incorrectly flagged when the method-call receiver contains an array creation expression with a nested constructor call (e.g.,x -> new Holder[] { new Holder() }[0].test(x)). Converting such a lambda to a method reference changes its semantics — the array and object would only be instantiated once at reference creation time rather than on every invocation.Root Cause
The rule did not account for cases where the receiver of the method call is an array access expression backed by an array creation with a nested constructor call. As a result, it treated these lambdas as safe to convert, producing a false positive.
Solution
Added an explicit check to detect when the method-call receiver involves an array creation expression containing a nested constructor call. Lambdas matching this pattern are now skipped by the rule.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)