Skip to content

[java] UseDiamondOperator: False positive with interdependent generic vars #5441

Closed

Description

@slovdahl

Affects PMD Version: 7.10.0 and 7.9.0 at least (but not 6.55.0)

Rule: UseDiamondOperator

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

Description:

In a slightly more complex chain of calls, PMD reports a UseDiamondOperator that at least Java 21 is unable to infer the generic types for if the suggestion from PMD is followed.

Code Sample demonstrating the issue:

package test;

import java.util.List;

class Test {
    interface CacheLoader<K, V> {
    }

    interface CacheBuilder<K, V> {
        <K1 extends K, V1 extends V> AsyncCache<K1, V1> buildAsync(CacheLoader<? super K1, V1> loader);
    }

    interface Cache<K, V> {
    }

    interface AsyncCache<K, V> {
        Cache<K, V> synchronous();
    }

    abstract static class ConcreteCacheLoader<K, V> implements CacheLoader<K, V> {
        abstract V fetch(K key);
    }

    interface Provider {
        <K, V> CacheBuilder<K, V> newCacheBuilder(String id);
    }

    void test(Provider provider) {
        Cache<String, List<Integer>> cache = provider.<String, List<Integer>>newCacheBuilder("test")
                .buildAsync(new ConcreteCacheLoader<String, List<Integer>>() {
                    @Override
                    List<Integer> fetch(String key) {
                        return List.of(123);
                    }
                })
                .synchronous();
    }
}

Expected outcome:

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

Test.java:30:  UseDiamondOperator:     Explicit type arguments can be replaced by a diamond: `new ConcreteCacheLoader<>()`

Running PMD through: Gradle 8.10

Activity

  1. added
    a:false-positive
    on Jan 2, 2025
  2. oowekyala commented on Feb 18, 2025

    @oowekyala
    Member

    Thanks for the report.

    It seems with a diamond, javac infers the ConcreteCacheLoader<> to CCL<Object, List<Integer>>, while we infer it to CCL<String, List<Integer>>. After investigating I think the issue lies pretty deep within our inference logic. More investigation needed...

  3. changed the title [-][java] UseDiamondOperator reported but Java 21 is unable to infer the type[/-] [+][java] UseDiamondOperator: False positive for anonymous class creation[/+] on Aug 14, 2026
  4. changed the title [-][java] UseDiamondOperator: False positive for anonymous class creation[/-] [+][java] UseDiamondOperator: False positive for anonymous class with interdependent generic vars[/+] on Aug 14, 2026
  5. changed the title [-][java] UseDiamondOperator: False positive for anonymous class with interdependent generic vars[/-] [+][java] UseDiamondOperator: False positive with interdependent generic vars[/+] on Aug 14, 2026
  6. added this to the 7.27.0 milestone on Aug 14, 2026
  7. added a commit that references this issue on Aug 14, 2026
    a9b6520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions