A RetroSearch Logo

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

Search Query:

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

[java] UnnecessaryCast false-positive for raw types · Issue #4822 · pmd/pmd · GitHub

Affects PMD Version:
7.0.0-rc4

Rule: UnnecessaryCast

Description:
False-positive as we need to cast without diamond operator to compile.

Code Sample demonstrating the issue:

import java.util.Objects;
import java.util.Comparator;
import static java.util.Comparator.nullsFirst;

public class Foo {
    public int compare(Comparable<?> o1, Comparable<?> o2) {
        final Comparator nullComparator = nullsFirst(null);
        if (o1 == null || o2 == null) {
            return Objects.compare(o1, o2, nullComparator);
        }
        Class<?> c1 = o1.getClass();
        Class<?> c2 = o2.getClass();
        if (c1 != c2) {
            return c1.getName().compareTo(c2.getName());
        }

        return ((Comparable) o1).compareTo(o2); // <--- Unnecessary cast (Comparable)
    }
}

Expected outcome:

PMD reports a violation at line ..., 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