Merge pull request #3065 from oowekyala:issue2876-ignored-annotation-bug
[java] Fix ignoredAnnotation property in UnusedPrivateField #3065
This commit is contained in:
@@ -38,6 +38,7 @@ This is a {{ site.pmd.release_type }} release.
|
||||
* [#575](https://github.com/pmd/pmd/issues/575): \[java] LiteralsFirstInComparisons should consider constant fields
|
||||
* [#2454](https://github.com/pmd/pmd/issues/2454): \[java] UnusedPrivateMethod violation for disabled class in 6.23.0
|
||||
* [#2833](https://github.com/pmd/pmd/issues/2833): \[java] NPE in UseCollectionIsEmptyRule with enums
|
||||
* [#2876](https://github.com/pmd/pmd/issues/2876): \[java] UnusedPrivateField cannot override ignored annotations property
|
||||
* java-codestyle
|
||||
* [#2960](https://github.com/pmd/pmd/issues/2960): \[java] Thread issue in MethodNamingConventionsRule
|
||||
* java-errorprone
|
||||
|
||||
@@ -42,16 +42,18 @@ public class UnusedPrivateFieldRule extends AbstractLombokAwareRule {
|
||||
|
||||
@Override
|
||||
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
||||
boolean classHasLombok = hasLombokAnnotation(node);
|
||||
if (hasIgnoredAnnotation(node)) {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope()
|
||||
.getDeclarations(VariableNameDeclaration.class);
|
||||
.getDeclarations(VariableNameDeclaration.class);
|
||||
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
|
||||
VariableNameDeclaration decl = entry.getKey();
|
||||
AccessNode accessNodeParent = decl.getAccessNodeParent();
|
||||
if (!accessNodeParent.isPrivate() || isOK(decl.getImage()) || classHasLombok
|
||||
|| hasIgnoredAnnotation((Annotatable) accessNodeParent)
|
||||
|| hasIgnoredAnnotation(node)) {
|
||||
if (!accessNodeParent.isPrivate()
|
||||
|| isOK(decl.getImage())
|
||||
|| hasIgnoredAnnotation((Annotatable) accessNodeParent)) {
|
||||
continue;
|
||||
}
|
||||
if (!actuallyUsed(entry.getValue())) {
|
||||
|
||||
@@ -636,6 +636,18 @@ public class Foo {
|
||||
<code><![CDATA[
|
||||
import lombok.EqualsAndHashCode;
|
||||
@EqualsAndHashCode
|
||||
public class Foo {
|
||||
private String bar;
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#2673 UnusedPrivateField false positive with lombok annotation EqualsAndHashCode</description>
|
||||
<rule-property name="ignoredAnnotations">lombok.Getter|lombok.Data</rule-property>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@RequiredArgsConstructor
|
||||
public class Foo {
|
||||
private String bar;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user