Skip to content

[java] UnnecessaryCast: False positives related to generics #6943

Closed

Description

@UncleOwen

Affects PMD Version: 7.27.0-SNAPSHOT

Rule: UnnecessaryCast

Description:

While trying to add UnnecessaryCast to the dogfood ruleset, I encountered the following FPs:

Code Sample demonstrating the issue:

String valueString = ((PropertyDescriptor) propertyDescriptor).serializer().toString(value);

https://github.com/pmd/pmd/blob/main/pmd-core/src/main/java/net/sourceforge/pmd/properties/AbstractPropertySource.java#L172

ShadowChainBuilder<S, ?>.ResolverBuilder builder = (ShadowChainBuilder<S, ?>.ResolverBuilder) classes.new ResolverBuilder();

https://github.com/pmd/pmd/blob/main/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/table/internal/JavaResolvers.java#L525

Expected outcome:

In each case, PMD reports a violation, but that's wrong. That's a false positive. Removing the cast results in a compile error.

Running PMD through: Maven

Activity

  1. added
    a:false-positive
    on Aug 6, 2026
  2. added a commit that references this issue on Aug 15, 2026
  3. added a commit that references this issue on Aug 21, 2026
    3e6d074
  4. added this to the 7.27.0 milestone on Aug 21, 2026
  5. added a commit that references this issue on Aug 21, 2026
    cd096e9
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