Skip to content

Commit 81d892e

Browse files
Godinleveretka
andauthored
SONARKT-507 Migrate VerifiedServerHostnamesCheck to kotlin-analysis-api
Co-authored-by: Marharyta Nedzelska <[email protected]> Co-authored-by: Evgeny Mandrikov <[email protected]>
1 parent 640ae4e commit 81d892e

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

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

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

19+
import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
1920
import org.jetbrains.kotlin.psi.KtExpression
2021
import org.jetbrains.kotlin.psi.KtLambdaExpression
2122
import org.jetbrains.kotlin.psi.KtNamedFunction
2223
import org.jetbrains.kotlin.psi.KtReturnExpression
23-
import org.jetbrains.kotlin.resolve.BindingContext
24-
import org.jetbrains.kotlin.resolve.calls.util.getParentCall
25-
import org.jetbrains.kotlin.resolve.calls.util.getType
2624
import org.sonar.check.Rule
2725
import org.sonarsource.kotlin.api.checks.AbstractCheck
2826
import org.sonarsource.kotlin.api.checks.FunMatcher
27+
import org.sonarsource.kotlin.api.checks.getParentCall
28+
import org.sonarsource.kotlin.api.checks.predictRuntimeBooleanValue
2929
import org.sonarsource.kotlin.api.frontend.KotlinFileContext
30+
import org.sonarsource.kotlin.api.visiting.withKaSession
3031

31-
@org.sonarsource.kotlin.api.frontend.K1only
3232
@Rule(key = "S5527")
3333
class VerifiedServerHostnamesCheck : AbstractCheck() {
3434

@@ -47,21 +47,19 @@ class VerifiedServerHostnamesCheck : AbstractCheck() {
4747
}
4848

4949
override fun visitNamedFunction(function: KtNamedFunction, kotlinFileContext: KotlinFileContext) {
50-
val (_, _, bindingContext) = kotlinFileContext
51-
if (VERIFY_MATCHER.matches(function, bindingContext)) {
50+
if (VERIFY_MATCHER.matches(function)) {
5251
val listStatements = function.listStatements()
53-
if (listStatements.size == 1 && onlyReturnsTrue(listStatements[0], bindingContext)) {
52+
if (listStatements.size == 1 && onlyReturnsTrue(listStatements[0])) {
5453
kotlinFileContext.reportIssue(function.nameIdentifier!!, MESSAGE)
5554
}
5655
}
5756
}
5857

5958
override fun visitLambdaExpression(expression: KtLambdaExpression, kotlinFileContext: KotlinFileContext) {
60-
val (_, _, bindingContext) = kotlinFileContext
61-
expression.getParentCall(bindingContext)?.let {
62-
if (HOSTNAME_VERIFIER_MATCHER.matches(it, bindingContext)) {
59+
(expression.getParentCall() as? KaFunctionCall<*>)?.let {
60+
if (HOSTNAME_VERIFIER_MATCHER.matches(it)) {
6361
val listStatements = expression.bodyExpression?.statements
64-
if (listStatements?.size == 1 && listStatements[0].isTrueConstant(bindingContext)) {
62+
if (listStatements?.size == 1 && listStatements[0].isTrueConstant()) {
6563
kotlinFileContext.reportIssue(expression, MESSAGE)
6664
}
6765
}
@@ -70,16 +68,13 @@ class VerifiedServerHostnamesCheck : AbstractCheck() {
7068

7169
private fun onlyReturnsTrue(
7270
ktExpression: KtExpression,
73-
bindingContext: BindingContext,
7471
): Boolean = when (ktExpression) {
7572
is KtReturnExpression ->
76-
ktExpression.returnedExpression?.isTrueConstant(bindingContext) ?: false
73+
ktExpression.returnedExpression?.isTrueConstant() ?: false
7774
else -> false
7875
}
7976

80-
private fun KtExpression.isTrueConstant(
81-
bindingContext: BindingContext,
82-
) = getType(bindingContext)?.let {
83-
bindingContext[BindingContext.COMPILE_TIME_VALUE, this]?.getValue(it) == true
84-
} ?: false
77+
private fun KtExpression.isTrueConstant() = withKaSession {
78+
predictRuntimeBooleanValue() ?: false
79+
}
8580
}

0 commit comments

Comments
 (0)