Skip to content

Commit

Permalink
Remove check for deprectated functions toUpperCase and toLowerCase
Browse files Browse the repository at this point in the history
toUpperCase and toLowerCase are deprecated, the ImplicitDefaultLocale should not check usage for them anymore

Closes detekt#6343
  • Loading branch information
Gosunet authored and BraisGabin committed Nov 9, 2023
1 parent 780fd3a commit acc0f1e
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,15 +8,12 @@ import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.internal.ActiveByDefault
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.builtins.KotlinBuiltIns.isStringOrNullableString
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getCalleeExpressionIfAny
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe

/**
Expand All @@ -32,18 +29,12 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
* String.format("Timestamp: %d", System.currentTimeMillis())
* "Timestamp: %d".format(System.currentTimeMillis())
*
* val str: String = getString()
* str.toUpperCase()
* str.toLowerCase()
* </noncompliant>
*
* <compliant>
* String.format(Locale.US, "Timestamp: %d", System.currentTimeMillis())
* "Timestamp: %d".format(Locale.US, System.currentTimeMillis())
*
* val str: String = getString()
* str.toUpperCase(Locale.US)
* str.toLowerCase(Locale.US)
* </compliant>
*/
@ActiveByDefault(since = "1.16.0")
Expand All @@ -63,7 +54,6 @@ class ImplicitDefaultLocale(config: Config = Config.empty) : Rule(config) {
override fun visitQualifiedExpression(expression: KtQualifiedExpression) {
super.visitQualifiedExpression(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.containsLocaleObject(bindingContext: BindingContext): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,72 +77,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 acc0f1e

Please sign in to comment.