Skip to content

Commit

Permalink
SONARKT-400 Migrate UnusedDeferredResultCheck to kotlin-analysis-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Dec 2, 2024
1 parent 643b3f7 commit 75018a6
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
*/
package org.sonarsource.kotlin.checks

import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement
import org.sonar.check.Rule
import org.sonarsource.kotlin.api.checks.AbstractCheck
import org.sonarsource.kotlin.api.checks.DEFERRED_FQN
import org.sonarsource.kotlin.api.checks.expressionTypeFqn
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
import org.sonarsource.kotlin.api.visiting.withKaSession

@org.sonarsource.kotlin.api.frontend.K1only
@Rule(key = "S6315")
class UnusedDeferredResultCheck : AbstractCheck() {

override fun visitCallExpression(expression: KtCallExpression, context: KotlinFileContext) {
val bindingContext = context.bindingContext
if (expression.expressionTypeFqn(bindingContext) == DEFERRED_FQN
&& expression.isUsedAsStatement(bindingContext)
) {
private val deferredFqn = ClassId.fromString(DEFERRED_FQN.replace('.', '/'))

override fun visitCallExpression(expression: KtCallExpression, context: KotlinFileContext) = withKaSession {
if (expression.expressionType!!.isClassType(deferredFqn)
&& !expression.isUsedAsExpression) {
context.reportIssue(expression.calleeExpression!!, """This function returns "Deferred", but its result is never used.""")
return
}
}
}

0 comments on commit 75018a6

Please sign in to comment.