Skip to content

Commit

Permalink
try without RequiresTypeResolution for compatibility (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Yatheesha Lokesha <yatheesha.lokesha@agoda.com>
  • Loading branch information
yathishbl60 and yatheesha-agoda authored Oct 15, 2024
1 parent 9d425f7 commit 9912196
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/main/kotlin/io/agodadev/kraftdetekt/IgnoredReturnValueRule.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package io.agodadev.kraftdetekt

import io.gitlab.arturbosch.detekt.api.*
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isUnit

const val IGNORED_RETURN_VALUE: String = "AG001: The return value of this function call is ignored. see https://github.com/agoda-com/agoda-kraft/blob/main/doc/AG001.md"

@RequiresTypeResolution
class IgnoredReturnValueRule(config: Config) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Expand All @@ -20,7 +17,6 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {
Debt.FIVE_MINS
)


override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)

Expand All @@ -29,38 +25,29 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {
}

val resolvedCall = expression.getResolvedCall(bindingContext) ?: return

val returnType = resolvedCall.resultingDescriptor.returnType ?: return

if (!returnType.isUnit() && !returnType.isNothing()) {
val parent = expression.parent
val ignoredExpressions = setOf(
KtValueArgument::class,
KtProperty::class,
KtReturnExpression::class,
KtDotQualifiedExpression::class,
KtWhenConditionWithExpression::class
)
when {
parent is KtValueArgument -> return
parent is KtProperty -> return
parent is KtReturnExpression -> return
parent is KtBinaryExpression && parent.operationToken.toString() == "EQ" -> return
parent::class in ignoredExpressions -> return
parent is KtBinaryExpression && parent.operationToken.toString() in setOf("EQ", "GT", "LT", "GTEQ", "LTEQ", "EQEQ", "EXCLEQ") -> return
parent is KtIfExpression && expression == parent.condition -> return
parent is KtWhenConditionWithExpression -> return
parent is KtBinaryExpression && parent.operationToken.toString() in setOf("GT", "LT", "GTEQ", "LTEQ", "EQEQ", "EXCLEQ") -> return
parent is KtDotQualifiedExpression -> return
parent is KtBlockExpression -> {
val isLastStatement = parent.statements.lastOrNull() == expression
val isOnlyStatement = parent.statements.size == 1
if (!isLastStatement && !isOnlyStatement) {
report(CodeSmell(
issue,
Entity.from(expression),
IGNORED_RETURN_VALUE
))
reportIgnoredReturnValue(expression)
}
}
else -> {
report(CodeSmell(
issue,
Entity.from(expression),
IGNORED_RETURN_VALUE
))
}
else -> reportIgnoredReturnValue(expression)
}
}
}
Expand All @@ -75,4 +62,12 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {
}
return false
}

private fun reportIgnoredReturnValue(expression: KtExpression) {
report(CodeSmell(
issue,
Entity.from(expression),
IGNORED_RETURN_VALUE
))
}
}

0 comments on commit 9912196

Please sign in to comment.