diff --git a/kotlin-checks-test-sources/src/main/kotlin/checks/EqualsOverriddenWithArrayFieldCheckSample.kt b/kotlin-checks-test-sources/src/main/kotlin/checks/EqualsOverriddenWithArrayFieldCheckSample.kt index 83057348b..bb6e32518 100644 --- a/kotlin-checks-test-sources/src/main/kotlin/checks/EqualsOverriddenWithArrayFieldCheckSample.kt +++ b/kotlin-checks-test-sources/src/main/kotlin/checks/EqualsOverriddenWithArrayFieldCheckSample.kt @@ -95,7 +95,7 @@ class EqualsOverriddenWithArrayFieldCheckSample { } } - data class WithInBodyProperty(val age: Int) { // Noncompliant {{Override equals and hashCode to consider array content in the method.}} + data class WithInBodyProperty(val age: Int) { // compliant val names: Array = arrayOf("Alice") override fun toString(): String { return "$names\n$age" @@ -156,10 +156,15 @@ class EqualsOverriddenWithArrayFieldCheckSample { data class EmptyBody(val names: Array) { // Noncompliant {{Override equals and hashCode to consider array content in the method.}} } - data class ArrayInBody(val age: Int) { // Noncompliant {{Override equals and hashCode to consider array content in the method.}} + data class ArrayInBody(val age: Int) { // Compliant val employers = arrayOf("SonarSource") } + data class ArrayGetterInBody(val age: Int) { // Compliant + val employers + get() = arrayOf("SonarSource") + } + data class NoArray(val age: Int) { // Compliant val employer = "SonarSource" } diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/EqualsOverriddenWithArrayFieldCheck.kt b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/EqualsOverriddenWithArrayFieldCheck.kt index 6c39f9d94..d3d8ffea4 100644 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/EqualsOverriddenWithArrayFieldCheck.kt +++ b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/EqualsOverriddenWithArrayFieldCheck.kt @@ -56,12 +56,8 @@ class EqualsOverriddenWithArrayFieldCheck : AbstractCheck() { private fun KtClass.hasAnArrayProperty(bindingContext: BindingContext): Boolean { // Because we only call this function on data classes, we can assume they have constructor val constructor = this.findDescendantOfType()!! - val oneParameterIsAnArray = constructor.valueParameters.any { it.isAnArray(bindingContext) } - return if (oneParameterIsAnArray) { - true - } else { - this.body?.properties?.any { it.isAnArray(bindingContext) } ?: false - } + // For data classes, only arguments of the primary constructor are included in equals and hashCode + return constructor.valueParameters.any { it.isAnArray(bindingContext) } } private fun KtClass.buildIssueMessage(bindingContext: BindingContext): String? {