PositionLiteralsFirstInComparisons don't take in account method call results. In the following example, the first line is detected but the second is not:
return x.equals("2");
return y.getX().equals("2");
I suggest the following replacement for the XPath rule and its corresponding example :
//PrimaryExpression
PrimaryPrefix[Name
[
(ends-with(@Image, '.equals'))
]
(../PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal)
and
( count(../PrimarySuffix/Arguments/ArgumentList/Expression) = 1 )
or (PrimarySuffix[@Image = 'equals']
and
(PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal)
and
( count(PrimarySuffix/Arguments/ArgumentList/Expression) = 1 ))
]
[not(ancestor::Expression/ConditionalAndExpression//EqualityExpression[@Image='!=']//NullLiteral)]
[not(ancestor::Expression/ConditionalOrExpression//EqualityExpression[@Image='==']//NullLiteral)]
class Foo {
boolean bar(String x) {
return x.equals("2"); // should be "2".equals(x)
}
boolean isValid(Person p) {
// wrong way
boolean b1 = p.getFirstName().equals("");
boolean b2 = p.getFirstName().trim().equals("");
return b1 || b2;
}
boolean isValid2(Person p) {
// right way
boolean b1 = "".equals(p.getFirstName());
boolean b2 = "".equals(p.getFirstName().trim());
return b1 || b2;
}
}