Skip to content

[java] UseDiamondOperator FP when expected type and constructed type have a different parameterization #2391

@krichter722

Description

@krichter722

Affects PMD Version:
6.22.0

Rule:
https://pmd.github.io/latest/pmd_rules_java_codestyle.html#usediamondoperator

Description:

Code Sample demonstrating the issue:

@FunctionalInterface
public interface LambdaKey<T> {

    String key(T value);
}

--- different file
public class LambdaKeyConverter<T> {

    public LambdaKeyConverter(final LambdaKey<T> lambdaKey) {
    }
}

--- different file
import java.util.List;

public class SomeClass {
    private final LambdaKeyConverter converter = new LambdaKeyConverter<List>(list -> String.valueOf(list.size()));
}

A SSCCE is provided at https://gitlab.com/krichter-sscce/pmd-diamond-operator-lambda-false-positive with CI output at https://gitlab.com/krichter-sscce/pmd-diamond-operator-lambda-false-positive/-/jobs/489958762

Expected outcome:
This is a false-positive as <List> is necessary in order to allow list.size() to compile.

Running PMD through: Maven

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions