resources.report.rules.pmd.UseLocaleWithCaseConversions.html Maven / Gradle / Ivy
UseLocaleWithCaseConversions
UseLocaleWithCaseConversions
When doing String.toLowerCase()/toUpperCase() conversions, use Locales to avoids problems with languages that have unusual conventions, i.e. Turkish.
//PrimaryExpression
[
PrimaryPrefix
[Name[ends-with(@Image, 'toLowerCase') or ends-with(@Image, 'toUpperCase')]]
[following-sibling::PrimarySuffix[position() = 1]/Arguments[@ArgumentCount=0]]
or
PrimarySuffix
[ends-with(@Image, 'toLowerCase') or ends-with(@Image, 'toUpperCase')]
[following-sibling::PrimarySuffix[position() = 1]/Arguments[@ArgumentCount=0]]
]
[not(PrimaryPrefix/Name[ends-with(@Image, 'toHexString')])]
Example(s):
class Foo {
// BAD
if (x.toLowerCase().equals("list")) { }
/*
This will not match "LIST" when in Turkish locale
The above could be
if (x.toLowerCase(Locale.US).equals("list")) { }
or simply
if (x.equalsIgnoreCase("list")) { }
*/
// GOOD
String z = a.toLowerCase(Locale.EN);
}