Skip to content

Commit

Permalink
SONARKT-423 Migrate ReceivingIntentsCheck to kotlin-analysis-api
Browse files Browse the repository at this point in the history
  • Loading branch information
leveretka authored and Godin committed Jan 20, 2025
1 parent 8882ff2 commit 616b470
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,17 @@ 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
?.getArgumentExpression()
?.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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.")
}
}
Expand Down

0 comments on commit 616b470

Please sign in to comment.