Skip to content

[java] Update UnusedPrivateFieldRule - ignore any annotations#4100

Merged
adangel merged 9 commits into
pmd:masterfrom
LynnBroe:master
Sep 30, 2022
Merged

[java] Update UnusedPrivateFieldRule - ignore any annotations#4100
adangel merged 9 commits into
pmd:masterfrom
LynnBroe:master

Conversation

@LynnBroe

@LynnBroe LynnBroe commented Aug 24, 2022

Copy link
Copy Markdown
Contributor

Describe the PR

I improve the rule UnusedPrivatefield and make the annotation @spybean can be detected

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

revise SpyBean about unusedprivatefield
@LynnBroe LynnBroe changed the title Update UnusedPrivateFieldRule.java: add a new rule to detect @SpyBean Update UnusedPrivateFieldRule.java: I improve the rule UnusedPrivatefield and make the annotation @SpyBean can be detected Aug 24, 2022
@adangel adangel self-requested a review August 24, 2022 16:02
@ghost

ghost commented Aug 29, 2022

Copy link
Copy Markdown
1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 55 violations, 2 errors and 6 configuration errors.
Full report
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 55 violations, 2 errors and 6 configuration errors.
Full report
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 6 configuration errors.
Full report
Compared to master:
This changeset changes 0 violations,
introduces 15 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report

Generated by 🚫 Danger

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR!

Please add a new test case to prove that #4037 is indeed fixed.

However, I think it's time to test another strategy for fixing these issues: Please try out what happens, if this rule just doesn't report any private fields, that has any annotations.
Because otherwise, we will keep adding one annotation after another, until every java framework is represented in this rule.
If this new strategy works and doesn't create too many false positives, then we would also have a fix for #4033 (and possible other issues).

@adangel adangel changed the title Update UnusedPrivateFieldRule.java: I improve the rule UnusedPrivatefield and make the annotation @SpyBean can be detected [java] Update UnusedPrivateFieldRule.java: I improve the rule UnusedPrivatefield and make the annotation @SpyBean can be detected Aug 29, 2022
@LynnBroe

LynnBroe commented Sep 5, 2022

Copy link
Copy Markdown
Contributor Author

Hi, adangel, I have added the test case, could you please help check it? Besides, I think it is inappropriate to dismiss all fields with annotations because many annotations may do not affect the current rule under checking. Fixing these issues is very easy, but dismissing them could lead to many false negatives. Thanks

@adangel adangel changed the title [java] Update UnusedPrivateFieldRule.java: I improve the rule UnusedPrivatefield and make the annotation @SpyBean can be detected [java] Update UnusedPrivateFieldRule - ignore annotation @SpyBean Sep 29, 2022
@adangel

adangel commented Sep 30, 2022

Copy link
Copy Markdown
Member

Thanks for the updated test case.

Besides, I think it is inappropriate to dismiss all fields with annotations because many annotations may do not affect the current rule under checking. Fixing these issues is very easy, but dismissing them could lead to many false negatives. Thanks

That's why I wanted to try this out to see the impact. I just did it - see the last regression tester report. It's not too bad IMHO.

Static code analysis always needs to balance false positives and false negatives. In that case - the rule is in category "best practices" - there is no big harm in having some false negatives (no program will crash because of an unused private field). Therefore I believe it is more important to reduce false positives than to avoid any false negatives. If the rule is too noisy it may lead to being ignored or disabled and maybe even removed from the ruleset entirely. In the end, the rule might not be used at all anymore.

I decided to deprecate the property "ignoreAnnotations". If there are good examples for false negatives, we might think of adding a new property to change the behavior, to deal with some annotations in a special way (e.g. "reportForAnnotations") or have a boolean flag (e.g. "reportRegardlessOfAnnotations"=true) which could be enabled if needed and would report all unused private fields whether annotated or not.

Having a more general solution also has the positive side effect, that #4033 is also fixed now.

@adangel adangel added this to the 6.50.0 milestone Sep 30, 2022
@adangel adangel changed the title [java] Update UnusedPrivateFieldRule - ignore annotation @SpyBean [java] Update UnusedPrivateFieldRule - ignore any annotations Sep 30, 2022
@adangel adangel merged commit 16bb510 into pmd:master Sep 30, 2022
adangel added a commit to adangel/pmd that referenced this pull request Sep 30, 2022
adangel added a commit to adangel/pmd that referenced this pull request Sep 30, 2022
[java] Update UnusedPrivateFieldRule - ignore any annotations pmd#4100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UnusedPrivateField - false positive with Spring @SpyBean [java] UnusedPrivateField - false positive with Lombok @ToString.Include

2 participants