Skip to content

Commit

Permalink
Remove check for deprectated functions toUpperCase and toLowerCase (#…
Browse files Browse the repository at this point in the history
…6548)

toUpperCase and toLowerCase are deprecated, the ImplicitDefaultLocale should not check usage for them anymore

Closes #6343
  • Loading branch information
Gosunet authored and cortinico committed Nov 25, 2023
1 parent f878a49 commit 274e742
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import org.jetbrains.kotlin.builtins.KotlinBuiltIns.isStringOrNullableString
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.resolve.calls.util.getCalleeExpressionIfAny
import org.jetbrains.kotlin.resolve.calls.util.getType

/**
* Prefer passing [java.util.Locale] explicitly than using implicit default value when formatting
Expand All @@ -29,17 +27,11 @@ import org.jetbrains.kotlin.resolve.calls.util.getType
* <noncompliant>
* String.format("Timestamp: %d", System.currentTimeMillis())
*
* val str: String = getString()
* str.toUpperCase()
* str.toLowerCase()
* </noncompliant>
*
* <compliant>
* String.format(Locale.US, "Timestamp: %d", System.currentTimeMillis())
*
* val str: String = getString()
* str.toUpperCase(Locale.US)
* str.toLowerCase(Locale.US)
* </compliant>
*/
@Suppress("ViolatesTypeResolutionRequirements")
Expand All @@ -56,13 +48,11 @@ class ImplicitDefaultLocale(config: Config = Config.empty) : Rule(config) {
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
super.visitDotQualifiedExpression(expression)
checkStringFormatting(expression)
checkCaseConversion(expression)
}

override fun visitSafeQualifiedExpression(expression: KtSafeQualifiedExpression) {
super.visitSafeQualifiedExpression(expression)
checkStringFormatting(expression)
checkCaseConversion(expression)
}

private fun checkStringFormatting(expression: KtQualifiedExpression) {
Expand All @@ -79,30 +69,6 @@ class ImplicitDefaultLocale(config: Config = Config.empty) : Rule(config) {
)
}
}

private fun checkCaseConversion(expression: KtQualifiedExpression) {
if (isStringOrNullableString(expression.receiverExpression.getType(bindingContext)) &&
expression.isCalleeCaseConversion() &&
expression.isCalleeNoArgs()
) {
report(
CodeSmell(
issue,
Entity.from(expression),
"${expression.text} uses implicitly default locale for case conversion."
)
)
}
}
}

private fun KtQualifiedExpression.isCalleeCaseConversion(): Boolean {
return getCalleeExpressionIfAny()?.text in arrayOf("toLowerCase", "toUpperCase")
}

private fun KtQualifiedExpression.isCalleeNoArgs(): Boolean {
val lastCallExpression = lastChild as? KtCallExpression
return lastCallExpression?.valueArguments.isNullOrEmpty()
}

private fun KtQualifiedExpression.containsStringTemplate(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,4 @@ class ImplicitDefaultLocaleSpec(private val env: KotlinCoreEnvironment) {
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `reports String_toUpperCase() call without explicit locale`() {
val code = """
fun x() {
val s = "deadbeef"
s.toUpperCase()
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

@Test
fun `does not report String_toUpperCase() call with explicit locale`() {
val code = """
import java.util.Locale
fun x() {
val s = "deadbeef"
s.toUpperCase(Locale.US)
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `reports String_toLowerCase() call without explicit locale`() {
val code = """
fun x() {
val s = "deadbeef"
s.toLowerCase()
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

@Test
fun `does not report String_toLowerCase() call with explicit locale`() {
val code = """
import java.util.Locale
fun x() {
val s = "deadbeef"
s.toLowerCase(Locale.US)
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}

@Test
fun `reports nullable String_toUpperCase call without explicit locale`() {
val code = """
fun x() {
val s: String? = "deadbeef"
s?.toUpperCase()
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}

@Test
fun `reports nullable String_toLowerCase call without explicit locale`() {
val code = """
fun x() {
val s: String? = "deadbeef"
s?.toLowerCase()
}
""".trimIndent()
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}

0 comments on commit 274e742

Please sign in to comment.