![JAR search and dependency download from the Maven repository](/logo.png)
resources.report.rules.pmd.UnusedNullCheckInEquals.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
The newest version!
UnusedNullCheckInEquals
UnusedNullCheckInEquals
After checking an object reference for null, you should invoke equals() on that object rather than passing it to another object's equals() method.
This rule is defined by the following XPath expression:
//PrimarySuffix[@Image='equals' and not(../PrimaryPrefix/Literal)]
/../PrimarySuffix/Arguments/ArgumentList/Expression
/PrimaryExpression/PrimaryPrefix
/Name[@Image = ./../../../../../../../../../../Expression/ConditionalAndExpression
/EqualityExpression[@Image="!=" and count(./preceding-sibling::*)=0 and
./PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
/PrimaryExpression/PrimaryPrefix/Name/@Image]
Example:
public class Test {
public String method1() { return "ok";}
public String method2() { return null;}
public void method(String a) {
String b;
/*
I don't know it method1() can be "null"
but I know "a" is not null..
I'd better write a.equals(method1())
*/
if (a!=null && method1().equals(a)) { // will
trigger the rule
//whatever
}
if (method1().equals(a) && a != null) { //
won't trigger the rule
//whatever
}
if (a!=null && method1().equals(b)) { // won't
trigger the rule
//whatever
}
if (a!=null && "LITERAL".equals(a)) { // won't
trigger the rule
//whatever
}
if (a!=null && !a.equals("go")) { // won't
trigger the rule
a=method2();
if (method1().equals(a)) {
//whatever
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy