Skip to content

Commit

Permalink
SONARKT-400 Migrate FlowChannelReturningFunsNotSuspendingCheck to kot…
Browse files Browse the repository at this point in the history
…lin-analysis-api
  • Loading branch information
Godin committed Dec 3, 2024
1 parent 3c5ba69 commit 9843de6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,21 @@ private fun KtTypeReference.determineType(bindingContext: BindingContext) =
fun KtTypeReference.determineTypeAsString(bindingContext: BindingContext, printTypeArguments: Boolean = false) =
determineType(bindingContext)?.getKotlinTypeFqName(printTypeArguments)

@Deprecated("use kotlin-analysis-api instead", ReplaceWith("this.returnTypeAsString()"))
fun KtNamedFunction.returnTypeAsString(bindingContext: BindingContext, printTypeArguments: Boolean = false) =
returnType(bindingContext)?.getKotlinTypeFqName(printTypeArguments)

@Deprecated("use kotlin-analysis-api", ReplaceWith("this.returnType"))
fun KtNamedFunction.returnType(bindingContext: BindingContext) =
createTypeBindingForReturnType(bindingContext)?.type

/**
* ```
* fun flexibleAkaPlatformType() = java.lang.String.valueOf(1)
* ```
*/
fun KtNamedFunction.returnTypeAsString(): String? = withKaSession { returnType.asFqNameString() }

fun KtLambdaArgument.isSuspending(
bindingContext: BindingContext,
) = (parent as? KtCallExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import org.sonarsource.kotlin.api.frontend.KotlinFileContext
private val FORBIDDEN_RETURN_TYPES = listOf(COROUTINES_FLOW, COROUTINES_CHANNEL)
private const val MESSAGE = """Functions returning "Flow" or "Channel" should not be suspending"""

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

override fun visitNamedFunction(function: KtNamedFunction, kotlinFileContext: KotlinFileContext) {
function.suspendModifier()?.let { suspendModifier ->
if (function.returnTypeAsString(kotlinFileContext.bindingContext) in FORBIDDEN_RETURN_TYPES) {
if (function.returnTypeAsString() in FORBIDDEN_RETURN_TYPES) {
val secondaries = function.typeReference
?.let { listOf(SecondaryLocation(kotlinFileContext.textRange(it))) }
?: emptyList()
Expand Down

0 comments on commit 9843de6

Please sign in to comment.