forked from phoedos/pmd
Fix 4457
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
package net.sourceforge.pmd.lang.java.rule.errorprone;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnonymousClassDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTImplementsList;
|
||||
@ -42,6 +43,25 @@ public class OverrideBothEqualsAndHashcodeRule extends AbstractJavaRule {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTAnonymousClassDeclaration node, Object data) {
|
||||
if (node.isInterface()) {
|
||||
return data;
|
||||
}
|
||||
super.visit(node, data);
|
||||
if (!implementsComparable && containsEquals ^ containsHashCode) {
|
||||
if (nodeFound == null) {
|
||||
nodeFound = node;
|
||||
}
|
||||
addViolation(data, nodeFound);
|
||||
}
|
||||
implementsComparable = false;
|
||||
containsEquals = false;
|
||||
containsHashCode = false;
|
||||
nodeFound = null;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTImplementsList node, Object data) {
|
||||
implementsComparable = node.children().filter(child -> TypeTestUtil.isA(Comparable.class, (TypeNode) child)).nonEmpty();
|
||||
|
@ -177,6 +177,25 @@ public class Foo implements C {
|
||||
public class Foo implements Runnable {
|
||||
public boolean equals(Object other) { return false; }
|
||||
public int compareTo(Object other) { return 42; }
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description> [java]A false negative about OverrideBothEqualsAndHashcode #4457 </description>
|
||||
<expected-problems>2</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public class B {
|
||||
Object o = new Object() {
|
||||
public boolean equals(Object other) { // report no warning
|
||||
return false;
|
||||
}
|
||||
};
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user