From aa56e367a4497aeff00ef1269f43b7dc00b3b5ff Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Tue, 8 Oct 2024 23:00:46 +0200 Subject: [PATCH] (WIP) RedundantSuspendModifierCheck --- .../kotlin/checks/RedundantSuspendModifierCheck.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/RedundantSuspendModifierCheck.kt b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/RedundantSuspendModifierCheck.kt index 5fe59c020..a7f232820 100644 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/RedundantSuspendModifierCheck.kt +++ b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/RedundantSuspendModifierCheck.kt @@ -19,11 +19,13 @@ */ package org.sonarsource.kotlin.checks -import org.jetbrains.kotlin.backend.common.descriptors.isSuspend +import org.jetbrains.kotlin.analysis.api.analyze +import org.jetbrains.kotlin.analysis.api.resolution.successfulFunctionCallOrNull +import org.jetbrains.kotlin.analysis.api.resolution.symbol +import org.jetbrains.kotlin.analysis.api.symbols.KaNamedFunctionSymbol import org.jetbrains.kotlin.psi.KtNameReferenceExpression import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType -import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall import org.sonar.check.Rule import org.sonarsource.kotlin.api.checks.AbstractCheck import org.sonarsource.kotlin.api.checks.overrides @@ -33,13 +35,15 @@ import org.sonarsource.kotlin.api.frontend.KotlinFileContext @Rule(key = "S6318") class RedundantSuspendModifierCheck : AbstractCheck() { - // TODO easy - override fun visitNamedFunction(function: KtNamedFunction, context: KotlinFileContext) { + override fun visitNamedFunction(function: KtNamedFunction, context: KotlinFileContext) = analyze(function) { val suspendModifier = function.suspendModifier() ?: return with(function) { if (hasBody() && !overrides()) { findDescendantOfType { - it.getResolvedCall(context.bindingContext)?.resultingDescriptor?.isSuspend ?: true + (it.resolveToCall()?.successfulFunctionCallOrNull() + ?.partiallyAppliedSymbol + ?.symbol as? KaNamedFunctionSymbol)?.isSuspend + ?: true } ?: context.reportIssue(suspendModifier, """Remove this unnecessary "suspend" modifier.""") } }