Skip to content

Commit a9debb7

Browse files
leveretkaGodin
authored andcommitted
SONARKT-400 Migrate ArrayHashCodeAndToStringCheck to kotlin-analysis-api
1 parent f15fcd2 commit a9debb7

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/ArrayHashCodeAndToStringCheck.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
*/
1717
package org.sonarsource.kotlin.checks
1818

19-
import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName
19+
import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
20+
import org.jetbrains.kotlin.analysis.api.resolution.symbol
21+
import org.jetbrains.kotlin.analysis.api.symbols.name
22+
import org.jetbrains.kotlin.analysis.api.types.symbol
2023
import org.jetbrains.kotlin.psi.KtCallExpression
21-
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
2224
import org.sonar.check.Rule
2325
import org.sonarsource.kotlin.api.checks.CallAbstractCheck
2426
import org.sonarsource.kotlin.api.checks.FunMatcher
2527
import org.sonarsource.kotlin.api.checks.FunMatcherImpl
26-
import org.sonarsource.kotlin.api.checks.determineType
2728
import org.sonarsource.kotlin.api.checks.predictReceiverExpression
2829
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
30+
import org.sonarsource.kotlin.api.visiting.withKaSession
2931

3032
private val OBJECT_ARRAY_MATCHER = FunMatcher(qualifier = "kotlin.Array") {
3133
withNames("hashCode", "toString")
@@ -52,22 +54,21 @@ private val PRIMITIVE_ARRAY_REPLACEMENT = mapOf("hashCode" to "contentHashCode",
5254
private val OBJECT_ARRAY_REPLACEMENT = mapOf("hashCode" to "contentDeepHashCode", "toString" to "contentDeepToString")
5355
private val ARRAY_OF_ARRAY_REPLACEMENT = mapOf("contentHashCode" to "contentDeepHashCode", "contentToString" to "contentDeepToString")
5456

55-
@org.sonarsource.kotlin.api.frontend.K1only
5657
@Rule(key = "S2116")
5758
class ArrayHashCodeAndToStringCheck : CallAbstractCheck() {
5859

5960
override val functionsToVisit = listOf(OBJECT_ARRAY_MATCHER, ARRAY_CONTENT_MATCHER) + PRIMITIVE_ARRAY_MATCHERS
6061

6162
override fun visitFunctionCall(
6263
callExpression: KtCallExpression,
63-
resolvedCall: ResolvedCall<*>,
64+
resolvedCall: KaFunctionCall<*>,
6465
matchedFun: FunMatcherImpl,
65-
kotlinFileContext: KotlinFileContext,
66+
kotlinFileContext: KotlinFileContext
6667
) {
67-
val methodName = resolvedCall.resultingDescriptor.name.asString()
68+
val methodName = resolvedCall.partiallyAppliedSymbol.symbol.name?.asString()
6869
val replacement = when (matchedFun) {
6970
OBJECT_ARRAY_MATCHER -> OBJECT_ARRAY_REPLACEMENT[methodName]
70-
ARRAY_CONTENT_MATCHER -> if (receiverIsArrayOfArray(callExpression, kotlinFileContext))
71+
ARRAY_CONTENT_MATCHER -> if (receiverIsArrayOfArray(callExpression))
7172
ARRAY_OF_ARRAY_REPLACEMENT[methodName] else null
7273

7374
else -> PRIMITIVE_ARRAY_REPLACEMENT[methodName]
@@ -77,11 +78,9 @@ class ArrayHashCodeAndToStringCheck : CallAbstractCheck() {
7778
}
7879
}
7980

80-
private fun receiverIsArrayOfArray(callExpression: KtCallExpression, kotlinFileContext: KotlinFileContext): Boolean {
81-
val bindingContext = kotlinFileContext.bindingContext
82-
return callExpression.predictReceiverExpression(bindingContext)?.determineType(bindingContext)?.arguments
83-
?.any { ARRAY_QUALIFIERS.contains(it.type.getKotlinTypeFqName(false)) }
84-
?: false
81+
private fun receiverIsArrayOfArray(callExpression: KtCallExpression): Boolean = withKaSession {
82+
val argument = callExpression.predictReceiverExpression()?.expressionType?.arrayElementType
83+
return ARRAY_QUALIFIERS.contains(argument?.symbol?.classId?.asFqNameString())
8584
}
8685

8786
}

0 commit comments

Comments
 (0)