From 42fa6ce6f00791194caec858bf3b769825982022 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 12 Dec 2018 15:41:16 +0300 Subject: [PATCH] Refactor "change type to mutable" in suspicious collection reassignment Related to KT-20626 --- ...SuspiciousCollectionReassignmentInspection.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/SuspiciousCollectionReassignmentInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/SuspiciousCollectionReassignmentInspection.kt index ce8835a2eb3..59c8b73f03a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/SuspiciousCollectionReassignmentInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/SuspiciousCollectionReassignmentInspection.kt @@ -84,12 +84,7 @@ class SuspiciousCollectionReassignmentInspection : AbstractKotlinInspection() { val initializer = property.initializer ?: return val fqName = initializer.resolveToCall()?.resultingDescriptor?.fqNameOrNull()?.asString() val psiFactory = KtPsiFactory(binaryExpression) - val mutableOf = when (fqName) { - "kotlin.collections.listOf" -> "mutableListOf" - "kotlin.collections.setOf" -> "mutableSetOf" - "kotlin.collections.mapOf" -> "mutableMapOf" - else -> null - } + val mutableOf = mutableConversionMap[fqName] if (mutableOf != null) { (initializer as? KtCallExpression)?.calleeExpression?.replaced(psiFactory.createExpression(mutableOf)) ?: return } else { @@ -113,6 +108,15 @@ class SuspiciousCollectionReassignmentInspection : AbstractKotlinInspection() { } companion object { + + private const val COLLECTIONS = "kotlin.collections" + + private val mutableConversionMap = mapOf( + "$COLLECTIONS.listOf" to "mutableListOf", + "$COLLECTIONS.setOf" to "mutableSetOf", + "$COLLECTIONS.mapOf" to "mutableMapOf" + ) + fun isApplicable(property: KtProperty): Boolean { return property.isLocal && property.initializer != null }