Skip to content

Commit

Permalink
SONARKT-423 Migrate DuplicateBranchCheck to kotlin-analysis-api
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka authored and Godin committed Jan 27, 2025
1 parent e2c9e19 commit 9fd1e62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -875,12 +875,15 @@ fun DeclarationDescriptor?.determineType(): KotlinType? =
else -> null
}

fun KtQualifiedExpression?.determineSignature(bindingContext: BindingContext): DeclarationDescriptor? =
when (val selectorExpr = this?.selectorExpression) {
is KtCallExpression -> bindingContext[BindingContext.REFERENCE_TARGET, selectorExpr.getCallNameExpression()]
is KtSimpleNameExpression -> bindingContext[BindingContext.REFERENCE_TARGET, selectorExpr]
fun KtQualifiedExpression?.determineSignature(): KaSymbol? = withKaSession {
when (val selectorExpr = this@determineSignature?.selectorExpression) {
is KtCallExpression ->
selectorExpr.getCallNameExpression()?.mainReference?.resolveToSymbol()
is KtSimpleNameExpression ->
selectorExpr.mainReference.resolveToSymbol()
else -> null
}
}

fun KtAnnotationEntry.annotatedElement(): KtAnnotated {
var annotated = parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package org.sonarsource.kotlin.checks
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.determineSignature
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
import org.sonarsource.kotlin.api.reporting.KotlinTextRanges.textRange
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
import org.sonarsource.kotlin.api.visiting.withKaSession

@org.sonarsource.kotlin.api.frontend.K1only
@Rule(key = "S1871")
class DuplicateBranchCheck : AbstractBranchDuplication() {

Expand All @@ -36,7 +35,7 @@ class DuplicateBranchCheck : AbstractBranchDuplication() {
group.asSequence()
.drop(1)
.filter { spansMultipleLines(it, ctx) }
.filter { it !is KtQualifiedExpression || it.hasSameSignature(original as KtQualifiedExpression, ctx.bindingContext) }
.filter { it !is KtQualifiedExpression || it.hasSameSignature(original as KtQualifiedExpression) }
.forEach { duplicated ->
val originalRange = ctx.textRange(original)
ctx.reportIssue(
Expand All @@ -53,14 +52,13 @@ class DuplicateBranchCheck : AbstractBranchDuplication() {
}
}

private fun KtQualifiedExpression.hasSameSignature(other: KtQualifiedExpression, bindingContext: BindingContext): Boolean =
this.determineSignature(bindingContext) == other.determineSignature(bindingContext)

private fun KtQualifiedExpression.hasSameSignature(other: KtQualifiedExpression): Boolean =
this@hasSameSignature.determineSignature() == other.determineSignature()

private fun spansMultipleLines(tree: KtElement, ctx: KotlinFileContext): Boolean {
if (tree is KtBlockExpression) {
val statements = tree.statements
if (statements.isNullOrEmpty()) {
if (statements.isEmpty()) {
return false
}
val firstStatement = statements[0]
Expand Down

0 comments on commit 9fd1e62

Please sign in to comment.