Skip to content

[java] UnusedPrivateField doesn't play well with UnnecessaryWarningSuppression #5785

@aaime

Description

@aaime

Affects PMD Version:

7.14.0

Description:

Adding UnnecessaryWarningSuppression to a custom ruleset causes UnusedPrivateField to be reported as unnecessary. Removing the suppression, PMD reports the field is unused.

Exception Stacktrace:

No stack trace

Code references/examples

Java code:
https://github.com/geotools/geotools/blob/main/modules/library/main/src/main/java/org/geotools/styling/ContrastEnhancementImpl.java#L70

The custom ruleset has:

<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>

Steps to reproduce:

Adding UnnecessaryWarningSuppression to a custom ruleset causes UnusedPrivateField to be reported as unnecessary. Removing the suppression, PMD reports the field is unused.

Running PMD through: Maven

Workaround

My guess is the issue arises because UnusedPrivateField, according to docs, ignores annotated private fields.
Indeed, changing the configuration as follows, resolves the issue, and UnnecessaryWarningSuppression no longer reports the suppression to be unnecessary:

  <rule ref="category/java/bestpractices.xml/UnusedPrivateField">
    <properties>
      <property name="reportForAnnotations" value="java.lang.SuppressWarnings" />
    </properties>
  </rule>

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugPMD crashes or fails to analyse a file.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions