#1128 CompareObjectsWithEquals False Positive comparing boolean (primitive) values
This commit is contained in:
@ -8,6 +8,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
||||
@ -50,6 +52,11 @@ public class CompareObjectsWithEqualsRule extends AbstractJavaRule {
|
||||
return data;
|
||||
}
|
||||
|
||||
// skip if either is part of a qualified name
|
||||
if (isPartOfQualifiedName(node.jjtGetChild(0)) || isPartOfQualifiedName(node.jjtGetChild(1))) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// skip static initializers... missing some cases here
|
||||
if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
|
||||
return data;
|
||||
@ -83,4 +90,16 @@ public class CompareObjectsWithEqualsRule extends AbstractJavaRule {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given node contains a qualified name, consisting of one
|
||||
* ASTPrimaryPrefix and one or more ASTPrimarySuffix nodes.
|
||||
*
|
||||
* @param node the node
|
||||
* @return <code>true</code> if it is a qualified name
|
||||
*/
|
||||
private boolean isPartOfQualifiedName(Node node) {
|
||||
return node.jjtGetChild(0) instanceof ASTPrimaryPrefix
|
||||
&& !node.findChildrenOfType(ASTPrimarySuffix.class).isEmpty();
|
||||
}
|
||||
}
|
||||
|
@ -150,4 +150,43 @@ public class Test {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#1128 CompareObjectsWithEquals False Positive comparing boolean (primitive) values</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class SonOfSomeClass extends SomeClass {
|
||||
protected javax.swing.JCheckBox checkBox;
|
||||
public class SomeEmbeddedClass {
|
||||
public boolean someNotWorkingMethod(boolean valid){
|
||||
// This line presents a CompareObjectsWithEquals violation
|
||||
valid |= SonOfSomeClass.this.object.isConfigurationEnabled() != SonOfSomeClass.this.checkBox.isSelected();
|
||||
return valid;
|
||||
}
|
||||
public boolean someWorkingMethod(boolean valid){
|
||||
// This line does not present any violation
|
||||
valid |= (SonOfSomeClass.this.object.isConfigurationEnabled()) != SonOfSomeClass.this.checkBox.isSelected();
|
||||
return valid;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
// just for reference
|
||||
class SomeClass {
|
||||
protected SomeObject object;
|
||||
}
|
||||
class SomeObject {
|
||||
private boolean configuration;
|
||||
public SomeObject() {
|
||||
super();
|
||||
}
|
||||
public boolean isConfigurationEnabled() {
|
||||
return configuration;
|
||||
}
|
||||
public void setConfiguration(boolean configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
*/
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
Reference in New Issue
Block a user