add PositionLiteralsFirstInCaseInsensitiveComparisons rule
most of this rule is the same as PositionLiteralsFirstInComparisons
This commit is contained in:
1 parent
32bf09ffd7
commit
6d5160a857
1 file changed
+48
@@ -1173,6 +1173,54 @@ class Foo {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
|
||||
<rule name="PositionLiteralsFirstInCaseInsensitiveComparisons"
|
||||
language="java"
|
||||
since="???"
|
||||
message="Position literals first in String comparisons for EqualsIgnoreCase"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#PositionLiteralsFirstInComparisons">
|
||||
<description>
|
||||
Position literals first in comparisons, if the second argument is null then NullPointerExceptions
|
||||
can be avoided, they will just return false.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//PrimaryExpression[
|
||||
PrimaryPrefix[Name
|
||||
[
|
||||
(ends-with(@Image, '.equalsIgnoreCase'))
|
||||
]
|
||||
]
|
||||
[
|
||||
(../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)]
|
||||
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
class Foo {
|
||||
boolean bar(String x) {
|
||||
return x.equalsIgnoreCase("2"); // should be "2".equalsIgnoreCase(x)
|
||||
}
|
||||
}
|
||||
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
|
||||
<rule name="UnnecessaryLocalBeforeReturn"
|
||||
since="3.3"
|
||||
message="Consider simply returning the value vs storing it in local variable ''{0}''"
|
||||
|
||||
Reference in new issue
Block a user