#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.ASTEqualityExpression;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
|
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
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.ast.ASTReferenceType;
|
||||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
||||||
@ -50,6 +52,11 @@ public class CompareObjectsWithEqualsRule extends AbstractJavaRule {
|
|||||||
return data;
|
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
|
// skip static initializers... missing some cases here
|
||||||
if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
|
if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
|
||||||
return data;
|
return data;
|
||||||
@ -83,4 +90,16 @@ public class CompareObjectsWithEqualsRule extends AbstractJavaRule {
|
|||||||
|
|
||||||
return data;
|
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>
|
]]></code>
|
||||||
</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>
|
</test-data>
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
**Bugfixes:**
|
**Bugfixes:**
|
||||||
|
|
||||||
|
* [#1128](https://sourceforge.net/p/pmd/bugs/1128/): CompareObjectsWithEquals False Positive comparing boolean (primitive) values
|
||||||
* [#1254](https://sourceforge.net/p/pmd/bugs/1254/): CPD run that worked in 5.1.2 fails in 5.1.3 with OOM
|
* [#1254](https://sourceforge.net/p/pmd/bugs/1254/): CPD run that worked in 5.1.2 fails in 5.1.3 with OOM
|
||||||
* [#1276](https://sourceforge.net/p/pmd/bugs/1276/): False positive in UnusedPrivateMethod with inner enum
|
* [#1276](https://sourceforge.net/p/pmd/bugs/1276/): False positive in UnusedPrivateMethod with inner enum
|
||||||
* [#1280](https://sourceforge.net/p/pmd/bugs/1280/): False Positive in UnusedImports when import used in javadoc
|
* [#1280](https://sourceforge.net/p/pmd/bugs/1280/): False Positive in UnusedImports when import used in javadoc
|
||||||
|
Reference in New Issue
Block a user