Skip to content

Commit

Permalink
(WIP) SimplifiedPreconditionsCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed Oct 8, 2024
1 parent 06b395d commit 2be04e0
Showing 1 changed file with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarsource.kotlin.checks

import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.analysis.api.resolution.KaFunctionCall
import org.jetbrains.kotlin.lexer.KtSingleValueToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
Expand All @@ -35,8 +36,6 @@ import org.jetbrains.kotlin.psi.KtThrowExpression
import org.jetbrains.kotlin.psi.KtWhenConditionWithExpression
import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.psiUtil.isNull
import org.jetbrains.kotlin.resolve.BindingContext
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.ConstructorMatcher
Expand Down Expand Up @@ -67,7 +66,7 @@ class SimplifiedPreconditionsCheck : CallAbstractCheck() {
}
)

override fun visitFunctionCall(callExpression: KtCallExpression, resolvedCall: ResolvedCall<*>, matchedFun: FunMatcherImpl, kotlinFileContext: KotlinFileContext) {
override fun visitFunctionCall(callExpression: KtCallExpression, resolvedCall: KaFunctionCall<*>, matchedFun: FunMatcherImpl, kotlinFileContext: KotlinFileContext) {
val conditionExpression = callExpression.valueArguments.first().getArgumentExpression()

if (conditionExpression.isNullCheckCondition(KtTokens.EXCLEQ)) {
Expand All @@ -81,15 +80,13 @@ class SimplifiedPreconditionsCheck : CallAbstractCheck() {
}

override fun visitThrowExpression(throwExpression: KtThrowExpression, kotlinFileContext: KotlinFileContext) {
val bindingContext = kotlinFileContext.bindingContext

when {
throwExpression.matchesException(bindingContext, ILLEGAL_STATE_EXCEPTION_CONSTRUCTOR_MATCH) -> {
throwExpression.matchesException(ILLEGAL_STATE_EXCEPTION_CONSTRUCTOR_MATCH) -> {
processException(throwExpression, kotlinFileContext, "check")
processExceptionForError(throwExpression, kotlinFileContext)
}

throwExpression.matchesException(bindingContext, ILLEGAL_ARGUMENT_EXCEPTION_CONSTRUCTOR_MATCH) ->
throwExpression.matchesException(ILLEGAL_ARGUMENT_EXCEPTION_CONSTRUCTOR_MATCH) ->
processException(throwExpression, kotlinFileContext, "require")
}
}
Expand Down Expand Up @@ -120,7 +117,7 @@ private fun KtExpression?.isNullCheckCondition(token: KtSingleValueToken) =

private fun KtBinaryExpression.getNullCheckVariable() = with(left!!) { if (isNull()) right!!.text else text }

private fun KtThrowExpression.matchesException(bindingContext: BindingContext, funMatcher: FunMatcherImpl) =
private fun KtThrowExpression.matchesException(funMatcher: FunMatcherImpl) =
(thrownExpression as? KtCallExpression)?.let { funMatcher.matches(it) } ?: false

private fun KtThrowExpression.getErrorMessage() =
Expand Down

0 comments on commit 2be04e0

Please sign in to comment.