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 ace18c952..d52e8fb41 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 @@ -676,6 +676,7 @@ fun Call.findCallInPrecedingCallChain(matcher: FunMatcherImpl, bindingContext: B return receiver to receiverResolved } +@Deprecated("use kotlin-analysis-api instead", ReplaceWith("this.isPredictedNull()")) fun ResolvedValueArgument.isNull(bindingContext: BindingContext) = ( (this as? ExpressionValueArgument) ?.valueArgument @@ -683,6 +684,9 @@ fun ResolvedValueArgument.isNull(bindingContext: BindingContext) = ( ?.predictRuntimeValueExpression(bindingContext) )?.isNull() ?: false +fun KtExpression.isPredictedNull() = + predictRuntimeValueExpression().isNull() + fun KtExpression.getCalleeOrUnwrappedGetMethod(bindingContext: BindingContext) = (this as? KtDotQualifiedExpression)?.let { dotQualifiedExpression -> (dotQualifiedExpression.selectorExpression as? KtNameReferenceExpression)?.let { nameSelector -> diff --git a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/ReceivingIntentsCheck.kt b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/ReceivingIntentsCheck.kt index c29f2fcec..cbf042458 100644 --- a/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/ReceivingIntentsCheck.kt +++ b/sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/ReceivingIntentsCheck.kt @@ -16,15 +16,14 @@ */ package org.sonarsource.kotlin.checks +import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall import org.jetbrains.kotlin.psi.KtCallExpression -import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.sonar.check.Rule import org.sonarsource.kotlin.api.checks.CallAbstractCheck import org.sonarsource.kotlin.api.checks.FunMatcher -import org.sonarsource.kotlin.api.checks.isNull +import org.sonarsource.kotlin.api.checks.isPredictedNull import org.sonarsource.kotlin.api.frontend.KotlinFileContext -@org.sonarsource.kotlin.api.frontend.K1only @Rule(key = "S5322") class ReceivingIntentsCheck : CallAbstractCheck() { override val functionsToVisit = listOf( @@ -33,11 +32,11 @@ class ReceivingIntentsCheck : CallAbstractCheck() { override fun visitFunctionCall( callExpression: KtCallExpression, - resolvedCall: ResolvedCall<*>, + resolvedCall: KaFunctionCall<*>, kotlinFileContext: KotlinFileContext ) { - val arguments = resolvedCall.valueArgumentsByIndex ?: return - if (arguments.size < 4 || arguments[2].isNull(kotlinFileContext.bindingContext)) { + val arguments = resolvedCall.argumentMapping.keys + if (arguments.size < 4 || arguments.elementAt(2).isPredictedNull()) { kotlinFileContext.reportIssue(callExpression.calleeExpression!!, "Make sure that intents are received safely here.") } }