Skip to content

[java] UnusedPrivateField - false positive with Lombok @ToString.Include #4033

@ghost

Description

Affects PMD Version:LATEST

Rule:UnusedPrivateField

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#unusedprivatefield

Description:
Hi, I found a false positive about the rule UnusedPrivateField, please read the following code example. @tostring is from lombok library Link, and it can generate a toString method to use the private field. Its similar annotations in lombok have been considered. Hence, I think we should consider this annotation.

I think we can add one more line lombok.ToString.Include in method defaultSuppressionAnnotations of UnusedPrivateFieldRule 😄

Code Sample demonstrating the issue:

@ToString
public class C {
    @ToString.Include
    private int a;  // Should not report a warning in this line
}

Expected outcome:
PMD should not report a violation at line 4, but doesn't. This is a false-positive.

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