Skip to content

Commit 9835bd4

Browse files
committed
SONARKT-400 Migrate RedundantMethodsInDataClassesCheck to kotlin-analysis-api
1 parent 08e998f commit 9835bd4

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.psi.KtNamedFunction
2828
import org.jetbrains.kotlin.psi.KtParameter
2929
import org.jetbrains.kotlin.psi.KtReturnExpression
3030
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
31-
import org.jetbrains.kotlin.resolve.BindingContext
3231
import org.sonar.check.Rule
3332
import org.sonarsource.kotlin.api.checks.ANY_TYPE
3433
import org.sonarsource.kotlin.api.checks.AbstractCheck
@@ -52,7 +51,6 @@ private val HASHCODE_MATCHER = FunMatcher {
5251
private val OBJECTS_HASH_MATCHER = FunMatcher(qualifier = "java.util.Objects", name = "hash")
5352
private val ARRAYS_HASHCODE_MATCHER = FunMatcher(qualifier = "java.util.Arrays", name = "hashCode")
5453

55-
@org.sonarsource.kotlin.api.frontend.K1only
5654
@Rule(key = "S6207")
5755
class RedundantMethodsInDataClassesCheck : AbstractCheck() {
5856

@@ -68,8 +66,8 @@ class RedundantMethodsInDataClassesCheck : AbstractCheck() {
6866

6967
klass.body?.functions?.forEach {
7068
when {
71-
EQUALS_MATCHER.matches(it, context.bindingContext) -> equalsMethod = it
72-
HASHCODE_MATCHER.matches(it, context.bindingContext) -> hashCodeMethod = it
69+
EQUALS_MATCHER.matches(it) -> equalsMethod = it
70+
HASHCODE_MATCHER.matches(it) -> hashCodeMethod = it
7371
}
7472
}
7573

@@ -80,7 +78,7 @@ class RedundantMethodsInDataClassesCheck : AbstractCheck() {
8078
}
8179

8280
hashCodeMethod?.let {
83-
if (it.hashCodeHasDefaultImpl(klassParameters, context.bindingContext)) {
81+
if (it.hashCodeHasDefaultImpl(klassParameters)) {
8482
context.reportIssue(it.nameIdentifier!!, issueMessage)
8583
}
8684
}
@@ -89,33 +87,31 @@ class RedundantMethodsInDataClassesCheck : AbstractCheck() {
8987

9088
private fun KtNamedFunction.hashCodeHasDefaultImpl(
9189
klassParameters: List<KtParameter>,
92-
bindingContext: BindingContext
9390
): Boolean {
9491
return if (hasBlockBody()) {
9592
val returnExpressions = collectDescendantsOfType<KtReturnExpression>()
9693
if (returnExpressions.size > 1) return false
97-
checkHashExpression(returnExpressions[0].returnedExpression, bindingContext, klassParameters)
94+
checkHashExpression(returnExpressions[0].returnedExpression, klassParameters)
9895
} else {
99-
checkHashExpression(this.bodyExpression, bindingContext, klassParameters)
96+
checkHashExpression(this.bodyExpression, klassParameters)
10097
}
10198
}
10299

103100
private fun checkHashExpression(
104101
expression: KtExpression?,
105-
bindingContext: BindingContext,
106102
klassParameters: List<KtParameter>
107103
): Boolean {
108104
if (expression !is KtDotQualifiedExpression) return false
109105
if (expression.selectorExpression !is KtCallExpression) return false
110106

111107
val callExpression = expression.selectorExpression as KtCallExpression
112-
if (OBJECTS_HASH_MATCHER.matches(callExpression, bindingContext)) {
108+
if (OBJECTS_HASH_MATCHER.matches(callExpression)) {
113109
if (callExpression.valueArguments.size != klassParameters.size) return false
114110
return callExpression.valueArguments.all {
115111
findParameter(it.getArgumentExpression(), klassParameters) != null
116112
}
117113
}
118-
if (ARRAYS_HASHCODE_MATCHER.matches(callExpression, bindingContext)) {
114+
if (ARRAYS_HASHCODE_MATCHER.matches(callExpression)) {
119115
val argumentExpression = callExpression.valueArguments[0].getArgumentExpression()
120116
if (argumentExpression !is KtCallExpression) return false
121117
val arguments = argumentExpression.valueArguments

0 commit comments

Comments
 (0)