A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5214 below:

[java] Wrong message for LambdaCanBeMethodReference with method of enclosing class · Issue #5214 · pmd/pmd · GitHub

Affects PMD Version: 7.5.0

Rule: LambdaCanBeMethodReference

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#lambdacanbemethodreference

Description:

PMD 7.5.0 flags this line: https://github.com/basepom/maven-plugins/blob/maven-plugins-v5/dependency-scope/src/main/java/org/basepom/mojo/dependencyscope/DependencyScopeMojo.java#L178

with

[WARNING] PMD Failure: org/basepom/mojo/dependencyscope/DependencyScopeMojo.java:178 Rule:LambdaCanBeMethodReference Priority:3 Lambda expression could be written as a method reference: `this::findViolations`.

implying that the code could be rewritten as

ListenableFuture<Set<DependencyViolation>> subfuture =
    subcontext.map(this::findViolations)
    .orElseGet(() -> Futures.immediateFuture(ImmutableSet.of()));

Code Sample demonstrating the issue:

    private ListenableFuture<Set<DependencyViolation>> findViolations(TraversalContext context) {
[...]
        Futures.addCallback(resolveArtifactDescriptor(context.currentArtifact()), new FutureCallback<>() {
[...]
            @Override
            public void onSuccess(ArtifactDescriptorResult artifactDescriptor) {
[...]
                        ListenableFuture<Set<DependencyViolation>> subfuture =
                                subcontext.map(traversalContext -> findViolations(traversalContext))
                                        .orElseGet(() -> Futures.immediateFuture(ImmutableSet.of()));
}

note that the map(traversalContext -> findViolations(traversalContext)) call is within a callback instance and has no access to the enclosing findViolations() method. However, PMD seems to get scope confused and flags this as being possible to rewrite as lambda (it is not, both IntelliJ and the java compiler refuse it).

Expected outcome:

PMD reports a violation at line 178, but that's wrong. That's a false positive.

Running PMD through: Maven


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4