diff --git a/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/checks/ApiExtensions.kt b/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/checks/ApiExtensions.kt index 69ab86129..d881497d8 100644 --- a/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/checks/ApiExtensions.kt +++ b/sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/checks/ApiExtensions.kt @@ -403,12 +403,26 @@ 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 +/** + * In the following example for the platform type `java.lang.String!` + * ``` + * fun flexibleAkaPlatformType() = java.lang.String.valueOf(1) + * ``` + * this function returns its lower bound `"java.lang.String"`. + * @see asFqNameString + */ +fun KtNamedFunction.returnTypeAsString(): String? = withKaSession { + returnType.asFqNameString() +} + fun KtLambdaArgument.isSuspending( bindingContext: BindingContext, ) = (parent as? KtCallExpression) diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/FlowChannelReturningFunsNotSuspendingCheck.kt b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/FlowChannelReturningFunsNotSuspendingCheck.kt index 6ecfaa1be..0ba76d486 100644 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/FlowChannelReturningFunsNotSuspendingCheck.kt +++ b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/FlowChannelReturningFunsNotSuspendingCheck.kt @@ -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()