17
17
package org.sonarsource.kotlin.checks
18
18
19
19
import com.intellij.psi.impl.source.tree.LeafPsiElement
20
+ import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
20
21
import org.jetbrains.kotlin.lexer.KtSingleValueToken
21
22
import org.jetbrains.kotlin.lexer.KtTokens
22
23
import org.jetbrains.kotlin.psi.KtBinaryExpression
@@ -32,8 +33,6 @@ import org.jetbrains.kotlin.psi.KtThrowExpression
32
33
import org.jetbrains.kotlin.psi.KtWhenConditionWithExpression
33
34
import org.jetbrains.kotlin.psi.KtWhenEntry
34
35
import org.jetbrains.kotlin.psi.psiUtil.isNull
35
- import org.jetbrains.kotlin.resolve.BindingContext
36
- import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
37
36
import org.sonar.check.Rule
38
37
import org.sonarsource.kotlin.api.checks.CallAbstractCheck
39
38
import org.sonarsource.kotlin.api.checks.ConstructorMatcher
@@ -52,7 +51,6 @@ private val ILLEGAL_ARGUMENT_EXCEPTION_CONSTRUCTOR_MATCH = ConstructorMatcher(ty
52
51
withArguments(" kotlin.String" )
53
52
}
54
53
55
- @org.sonarsource.kotlin.api.frontend.K1only
56
54
@Rule(key = " S6532" )
57
55
class SimplifiedPreconditionsCheck : CallAbstractCheck () {
58
56
@@ -65,7 +63,8 @@ class SimplifiedPreconditionsCheck : CallAbstractCheck() {
65
63
}
66
64
)
67
65
68
- override fun visitFunctionCall (callExpression : KtCallExpression , resolvedCall : ResolvedCall <* >, matchedFun : FunMatcherImpl , kotlinFileContext : KotlinFileContext ) {
66
+
67
+ override fun visitFunctionCall (callExpression : KtCallExpression , resolvedCall : KaFunctionCall <* >, kotlinFileContext : KotlinFileContext ) {
69
68
val conditionExpression = callExpression.valueArguments.first().getArgumentExpression()
70
69
71
70
if (conditionExpression.isNullCheckCondition(KtTokens .EXCLEQ )) {
@@ -79,15 +78,13 @@ class SimplifiedPreconditionsCheck : CallAbstractCheck() {
79
78
}
80
79
81
80
override fun visitThrowExpression (throwExpression : KtThrowExpression , kotlinFileContext : KotlinFileContext ) {
82
- val bindingContext = kotlinFileContext.bindingContext
83
-
84
81
when {
85
- throwExpression.matchesException(bindingContext, ILLEGAL_STATE_EXCEPTION_CONSTRUCTOR_MATCH ) -> {
82
+ throwExpression.matchesException(ILLEGAL_STATE_EXCEPTION_CONSTRUCTOR_MATCH ) -> {
86
83
processException(throwExpression, kotlinFileContext, " check" )
87
84
processExceptionForError(throwExpression, kotlinFileContext)
88
85
}
89
86
90
- throwExpression.matchesException(bindingContext, ILLEGAL_ARGUMENT_EXCEPTION_CONSTRUCTOR_MATCH ) ->
87
+ throwExpression.matchesException(ILLEGAL_ARGUMENT_EXCEPTION_CONSTRUCTOR_MATCH ) ->
91
88
processException(throwExpression, kotlinFileContext, " require" )
92
89
}
93
90
}
@@ -118,8 +115,8 @@ private fun KtExpression?.isNullCheckCondition(token: KtSingleValueToken) =
118
115
119
116
private fun KtBinaryExpression.getNullCheckVariable () = with (left!! ) { if (isNull()) right!! .text else text }
120
117
121
- private fun KtThrowExpression.matchesException (bindingContext : BindingContext , funMatcher : FunMatcherImpl ) =
122
- (thrownExpression as ? KtCallExpression )?.let { funMatcher.matches(it, bindingContext ) } ? : false
118
+ private fun KtThrowExpression.matchesException (funMatcher : FunMatcherImpl ) =
119
+ (thrownExpression as ? KtCallExpression )?.let { funMatcher.matches(it) } ? : false
123
120
124
121
private fun KtThrowExpression.getErrorMessage () =
125
122
(thrownExpression as ? KtCallExpression )?.valueArguments.let { if (it?.size == 1 ) it[0 ].text else null }
0 commit comments