Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARKT-379 Fix false positive in S6218 #375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = arrayOf("Alice")
override fun toString(): String {
return "$names\n$age"
Expand Down Expand Up @@ -156,10 +156,15 @@ class EqualsOverriddenWithArrayFieldCheckSample {
data class EmptyBody(val names: Array<String>) { // 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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<KtPrimaryConstructor>()!!
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? {
Expand Down