forked from phoedos/pmd
false + in CompareObjectsWithEquals for allocation expressions such as 'new File(b).exists() == false'
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4936 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -123,5 +123,18 @@ public class Foo {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
qualified call in allocation
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
boolean bar(String b) {
|
||||
return new File(b).exists() == false;
|
||||
}
|
||||
}
|
||||
</test-data>
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
||||
|
@ -23,25 +23,27 @@ public class CompareObjectsWithEquals extends AbstractRule {
|
||||
* @return true if child 0 is an AllocationExpression
|
||||
*/
|
||||
private boolean isAllocation(Node n) {
|
||||
return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTAllocationExpression;
|
||||
return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTAllocationExpression && n.jjtGetParent().jjtGetNumChildren() == 1;
|
||||
}
|
||||
|
||||
public Object visit(ASTEqualityExpression node, Object data) {
|
||||
Node c0 = node.jjtGetChild(0).jjtGetChild(0);
|
||||
Node c1 = node.jjtGetChild(1).jjtGetChild(0);
|
||||
|
||||
// If either side is allocating a new object, there's no way an
|
||||
// equals expression is correct
|
||||
if (isAllocation(node.jjtGetChild(0).jjtGetChild(0)) || isAllocation(node.jjtGetChild(1).jjtGetChild(0))) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
|
||||
// equals expression is correct
|
||||
if ((isAllocation(c0)) || (isAllocation(c1))) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
|
||||
// skip if either child is not a simple name
|
||||
if (!hasName(((SimpleNode) node.jjtGetChild(0)).jjtGetChild(0)) || !hasName(((SimpleNode) node.jjtGetChild(1)).jjtGetChild(0))) {
|
||||
if (!hasName(c0) || !hasName(c1)) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// skip if either is a qualified name
|
||||
if (isQualifiedName((SimpleNode) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0))
|
||||
|| isQualifiedName((SimpleNode) node.jjtGetChild(1).jjtGetChild(0).jjtGetChild(0))) {
|
||||
if (isQualifiedName((SimpleNode) c0.jjtGetChild(0)) || isQualifiedName((SimpleNode) c1.jjtGetChild(0))) {
|
||||
return data;
|
||||
}
|
||||
|
||||
@ -49,9 +51,9 @@ public class CompareObjectsWithEquals extends AbstractRule {
|
||||
if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
|
||||
return data;
|
||||
}
|
||||
|
||||
ASTName n0 = (ASTName) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
|
||||
ASTName n1 = (ASTName) node.jjtGetChild(1).jjtGetChild(0).jjtGetChild(0);
|
||||
|
||||
ASTName n0 = (ASTName) c0.jjtGetChild(0);
|
||||
ASTName n1 = (ASTName) c1.jjtGetChild(0);
|
||||
|
||||
if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) {
|
||||
VariableNameDeclaration nd0 = (VariableNameDeclaration) n0.getNameDeclaration();
|
||||
|
Reference in New Issue
Block a user