Skip to main content Link Search Menu Expand Document (external link)

StringCaseLocaleUsage

WARNING

FragileCode

View source code on GitHub

Summary

Specify a Locale when calling String#to{Lower,Upper}Case

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringCaseLocaleUsage") to the enclosing element.

Disable this pattern completely by adding -Xep:StringCaseLocaleUsage:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

+import java.util.Locale;
+
 class A {
   void m() {
-    "a".toLowerCase(/* Comment with parens: (). */ );
-    "b".toUpperCase();
-    "c".toLowerCase().toString();
+    "a".toLowerCase(/* Comment with parens: (). */ Locale.ROOT);
+    "b".toUpperCase(Locale.ROOT);
+    "c".toLowerCase(Locale.ROOT).toString();
 
-    toString().toLowerCase();
-    toString().toUpperCase /* Comment with parens: (). */();
+    toString().toLowerCase(Locale.ROOT);
+    toString().toUpperCase /* Comment with parens: (). */(Locale.ROOT);
 
-    this.toString().toLowerCase() /* Comment with parens: (). */;
-    this.toString().toUpperCase();
+    this.toString().toLowerCase(Locale.ROOT) /* Comment with parens: (). */;
+    this.toString().toUpperCase(Locale.ROOT);
   }
 }
+import java.util.Locale;
+
 class A {
   void m() {
-    "a".toLowerCase();
-    "b".toUpperCase(/* Comment with parens: (). */ );
-    "c".toLowerCase().toString();
+    "a".toLowerCase(Locale.getDefault());
+    "b".toUpperCase(/* Comment with parens: (). */ Locale.getDefault());
+    "c".toLowerCase(Locale.getDefault()).toString();
 
-    toString().toLowerCase();
-    toString().toUpperCase /* Comment with parens: (). */();
+    toString().toLowerCase(Locale.getDefault());
+    toString().toUpperCase /* Comment with parens: (). */(Locale.getDefault());
 
-    this.toString().toLowerCase() /* Comment with parens: (). */;
-    this.toString().toUpperCase();
+    this.toString().toLowerCase(Locale.getDefault()) /* Comment with parens: (). */;
+    this.toString().toUpperCase(Locale.getDefault());
   }
 }

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import static java.util.Locale.ROOT;

import java.util.Locale;

class A {
  void m() {
    "a".toLowerCase(Locale.ROOT);
    "a".toUpperCase(Locale.ROOT);
    "b".toLowerCase(ROOT);
    "b".toUpperCase(ROOT);
    "c".toLowerCase(Locale.getDefault());
    "c".toUpperCase(Locale.getDefault());
    "d".toLowerCase(Locale.ENGLISH);
    "d".toUpperCase(Locale.ENGLISH);
    "e".toLowerCase(new Locale("foo"));
    "e".toUpperCase(new Locale("foo"));

    // BUG: Diagnostic contains:
    "f".toLowerCase();
    // BUG: Diagnostic contains:
    "g".toUpperCase();

    String h = "h";
    // BUG: Diagnostic contains:
    h.toLowerCase();
    String i = "i";
    // BUG: Diagnostic contains:
    i.toUpperCase();
  }
}

Copyright © 2017-2023 Picnic Technologies BV