Skip to content

Commit 2269c53

Browse files
leveretkaGodin
authored andcommitted
SONARKT-400 Migrate EncryptionAlgorithmCheck to kotlin-analysis-api
1 parent e0bf3a7 commit 2269c53

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/checks/ApiExtensions.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ fun KtExpression.predictRuntimeStringValue(bindingContext: BindingContext) =
139139
fun KtExpression.predictRuntimeStringValue() =
140140
predictRuntimeValueExpression().stringValue()
141141

142-
fun KtExpression.predictRuntimeStringValueWithSecondaries(bindingContext: BindingContext) =
142+
fun KtExpression.predictRuntimeStringValueWithSecondaries() = withKaSession {
143143
mutableListOf<PsiElement>().let {
144-
predictRuntimeValueExpression(bindingContext, it)
145-
.stringValue(bindingContext, it) to it
144+
predictRuntimeValueExpression(it)
145+
.stringValue(it) to it
146146
}
147+
}
147148

148149
@Deprecated("use kotlin-analysis-api instead", ReplaceWith("this.predictRuntimeIntValue()"))
149150
fun KtExpression.predictRuntimeIntValue(bindingContext: BindingContext) =

sonar-kotlin-checks/src/main/java/org/sonarsource/kotlin/checks/EncryptionAlgorithmCheck.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
*/
1717
package org.sonarsource.kotlin.checks
1818

19+
import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
1920
import org.jetbrains.kotlin.psi.KtCallExpression
20-
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
2121
import org.sonar.check.Rule
2222
import org.sonarsource.kotlin.api.checks.CallAbstractCheck
2323
import org.sonarsource.kotlin.api.checks.FunMatcher
24-
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
24+
import org.sonarsource.kotlin.api.checks.FunMatcherImpl
2525
import org.sonarsource.kotlin.api.checks.predictRuntimeStringValueWithSecondaries
26+
import org.sonarsource.kotlin.api.reporting.SecondaryLocation
2627
import org.sonarsource.kotlin.api.reporting.KotlinTextRanges.textRange
2728
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
2829

@@ -33,21 +34,20 @@ val CIPHER_GET_INSTANCE_MATCHER = FunMatcher {
3334
name = "getInstance"
3435
}
3536

36-
@org.sonarsource.kotlin.api.frontend.K1only
3737
@Rule(key = "S5542")
3838
class EncryptionAlgorithmCheck : CallAbstractCheck() {
3939

4040
override val functionsToVisit = listOf(CIPHER_GET_INSTANCE_MATCHER)
4141

4242
override fun visitFunctionCall(
4343
callExpression: KtCallExpression,
44-
resolvedCall: ResolvedCall<*>,
45-
kotlinFileContext: KotlinFileContext,
44+
resolvedCall: KaFunctionCall<*>,
45+
matchedFun: FunMatcherImpl,
46+
kotlinFileContext: KotlinFileContext
4647
) {
47-
val bindingContext = kotlinFileContext.bindingContext
4848
callExpression.valueArguments.firstOrNull()?.let { argument ->
4949
argument.getArgumentExpression()!!
50-
.predictRuntimeStringValueWithSecondaries(bindingContext).let { (algorithm, secondaries) ->
50+
.predictRuntimeStringValueWithSecondaries().let { (algorithm, secondaries) ->
5151
algorithm?.getInsecureAlgorithmMessage()?.let { errorMessage ->
5252
val locations = secondaries.map { secondaryLocation ->
5353
SecondaryLocation(kotlinFileContext.textRange(secondaryLocation), "Transformation definition")

0 commit comments

Comments
 (0)