Kotlin

Kotlin

Made by DeepSource

Implicit default locale used for string processing KT-W1030

Bug risk
Minor

The code uses the implicit default locale for string processing. By relying on the default locale, the code becomes susceptible to variations in behavior across different systems, which can result in inconsistent and unpredictable results. It is recommended to explicitly specify the locale to ensure consistent and predictable behavior.

To fix this issue, explicitly specify the desired locale when performing string processing operations. This can be done by using methods that accept a Locale parameter or by setting the default locale explicitly using Locale.setDefault().

Bad Practice

fun someFunction() {
  val str = "Hello"
  val upperCaseStr = str.toUpperCase() // Implicit default locale used
  val formattedString = String.format("Timestamp: %d", System.currentTimeMillis()) // Implicit default locale used
}

Recommended

fun someFunction() {
  val str = "Hello"
  val upperCaseStr = str.toUpperCase(Locale.US) // Explicitly specify the locale
  val formattedString = String.format(Locale.US, "Timestamp: %d", System.currentTimeMillis()) // Explicitly specify the locale
}