Applied patch 1612455 from Jason B

RFE 1411022 CompareObjectsWithEquals now catches the case where comparison is against new Object


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4914 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2007-01-03 02:45:46 +00:00
parent dfd2dc3622
commit 4f64aaa7a5
4 changed files with 36 additions and 1 deletions

View File

@ -2,6 +2,7 @@
Fixed bug 1618858 - PMD no longer raises an exception on XPath like '//ConditionalExpression//ConditionalExpression'
Fixed bug 1626232 - Commons logging rules (ProperLogger and UseCorrectExceptionLogging) now catch more cases
Fixed bug 1626201 - BrokenNullCheck now catches more cases
Applied patch 1612455 - RFE 1411022 CompareObjectsWithEquals now catches the case where comparison is against new Object
December 19, 2006 - 3.9:
New rules:

View File

@ -109,4 +109,19 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Comparing against new object should always return false
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar(Date aDate) {
if ( aDate == new Date( 0 ) ) {
}
}
}
]]></code>
</test-code>
}
</test-data>

View File

@ -1,6 +1,7 @@
package net.sourceforge.pmd.rules.design;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTAllocationExpression;
import net.sourceforge.pmd.ast.ASTEqualityExpression;
import net.sourceforge.pmd.ast.ASTInitializer;
import net.sourceforge.pmd.ast.ASTName;
@ -14,7 +15,25 @@ public class CompareObjectsWithEquals extends AbstractRule {
return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTName;
}
/**
* Indicate whether this node is allocating a new object.
*
* @param n
* node that might be allocating a new object
* @return true if child 0 is an AllocationExpression
*/
private boolean isAllocation(Node n) {
return n.jjtGetNumChildren() > 0 && n.jjtGetChild(0) instanceof ASTAllocationExpression;
}
public Object visit(ASTEqualityExpression node, Object data) {
// 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;
}
// skip if either child is not a simple name
if (!hasName(((SimpleNode) node.jjtGetChild(0)).jjtGetChild(0)) || !hasName(((SimpleNode) node.jjtGetChild(1)).jjtGetChild(0))) {
return data;

View File

@ -56,7 +56,7 @@
<ul>
<li>Ryan Gustafson - Patch to add more annotation suppression tests, patch to fix bug in AvoidDecimalLiteralsInBigDecimalConstructor, patch to add "ref" overrides to RuleSetFactory, patch to fix JDK 1.3 incompatibilities in PMD 2.0, patch to support classpaths with spaces in pmd.bat, patch to fix controversial/DefaultPackage XPath rule</li>
<li>Lukas Theussl - Patch to bring Maven configuration files up to date</li>
<li>Jason Bennett - Rewrite of annotation-based warning suppression to allow for rule-specific suppression, noticed useless line in XSLT scripts, fix for UnnecessaryLocalBeforeReturn, wrote NPathComplexity rule, patches to improve CyclomaticComplexity rule, Implemented: UseCollectionIsEmpty, NcssTypeCount, NcssMethodCount, NcssConstructor</li>
<li>Jason Bennett - Rewrite of annotation-based warning suppression to allow for rule-specific suppression, noticed useless line in XSLT scripts, fix for UnnecessaryLocalBeforeReturn, wrote NPathComplexity rule, patches to improve CyclomaticComplexity rule, Implemented: UseCollectionIsEmpty, NcssTypeCount, NcssMethodCount, NcssConstructor, Patch to detect comparison with new Object</li>
<li>Brent Fisher - Fixed report backslash bug, SummaryHTML report improvements</li>
<li>Thomas Leplus - Rewrote UselessStringValueOf</li>
<li>Larry Brigman - Reported symlink bug in CPD</li>