Skip to content

Commit 76827a9

Browse files
committed
SONARKT-400 Migrate EqualsOverriddenWithArrayFieldCheck to kotlin-analysis-api
1 parent 9835bd4 commit 76827a9

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi.KtPrimaryConstructor
2323
import org.jetbrains.kotlin.psi.KtProperty
2424
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
2525
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
26-
import org.jetbrains.kotlin.resolve.BindingContext
2726
import org.sonar.check.Rule
2827
import org.sonarsource.kotlin.api.checks.AbstractCheck
2928
import org.sonarsource.kotlin.api.checks.ArgumentMatcher
@@ -41,31 +40,30 @@ private val EXPECTED_OVERRIDES = listOf(
4140
}
4241
)
4342

44-
@org.sonarsource.kotlin.api.frontend.K1only
4543
@Rule(key = "S6218")
4644
class EqualsOverriddenWithArrayFieldCheck : AbstractCheck() {
4745
override fun visitClass(klass: KtClass, context: KotlinFileContext) {
48-
if (!klass.isData() || !klass.hasAnArrayProperty(context.bindingContext)) {
46+
if (!klass.isData() || !klass.hasAnArrayProperty()) {
4947
return
5048
}
51-
klass.buildIssueMessage(context.bindingContext)?.let { context.reportIssue(klass.nameIdentifier!!, it) }
49+
klass.buildIssueMessage()?.let { context.reportIssue(klass.nameIdentifier!!, it) }
5250
}
5351

54-
private fun KtClass.hasAnArrayProperty(bindingContext: BindingContext): Boolean {
52+
private fun KtClass.hasAnArrayProperty(): Boolean {
5553
// Because we only call this function on data classes, we can assume they have constructor
5654
val constructor = this.findDescendantOfType<KtPrimaryConstructor>()!!
57-
val oneParameterIsAnArray = constructor.valueParameters.any { it.isAnArray(bindingContext) }
55+
val oneParameterIsAnArray = constructor.valueParameters.any { it.isAnArray() }
5856
return if (oneParameterIsAnArray) {
5957
true
6058
} else {
61-
this.body?.properties?.any { it.isAnArray(bindingContext) } ?: false
59+
this.body?.properties?.any { it.isAnArray() } ?: false
6260
}
6361
}
6462

65-
private fun KtClass.buildIssueMessage(bindingContext: BindingContext): String? {
63+
private fun KtClass.buildIssueMessage(): String? {
6664
val functions = this.collectOverridingFunctions()
6765
val missingFunctionNames = EXPECTED_OVERRIDES
68-
.filter { matcher -> !functions.any { function -> matcher.matches(function, bindingContext) } }
66+
.filter { matcher -> !functions.any { function -> matcher.matches(function) } }
6967
.map { it.names.first() }
7068
return when (missingFunctionNames.size) {
7169
1 -> "Override ${missingFunctionNames[0]} to consider array content in the method."
@@ -78,9 +76,9 @@ class EqualsOverriddenWithArrayFieldCheck : AbstractCheck() {
7876
this.collectDescendantsOfType<KtNamedFunction>()
7977
.filter { it.overrides() }
8078

81-
private fun KtParameter.isAnArray(bindingContext: BindingContext): Boolean =
82-
this.determineTypeAsString(bindingContext, printTypeArguments = false) == "kotlin.Array"
79+
private fun KtParameter.isAnArray(): Boolean =
80+
this.determineTypeAsString() == "kotlin.Array"
8381

84-
private fun KtProperty.isAnArray(bindingContext: BindingContext): Boolean =
85-
this.determineTypeAsString(bindingContext, printTypeArguments = false) == "kotlin.Array"
82+
private fun KtProperty.isAnArray(): Boolean =
83+
this.determineTypeAsString() == "kotlin.Array"
8684
}

0 commit comments

Comments
 (0)