Rule to ensure callee of function returning Try<Foo, Throwable> handles the throwable #3536
-
Which of the This is what I have so far (based off /* @requiresTypeResolution */
class DanglingTry(config: Config) : Rule(config) {
override val issue = Issue(
"DanglingTry",
Severity.Defect,
"callee of Try does not handle Throwable",
Debt.TEN_MINS
)
override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)
if (bindingContext == BindingContext.EMPTY) return
if (expression.isUsedAsExpression(bindingContext)) return
val resolvedCall = expression.getResolvedCall(bindingContext) ?: return
val returnType = resolvedCall.resultingDescriptor.returnType ?: return
if (returnType.getJetTypeFqName(false) != "com.acme.Try") return
val entity = Entity.from(expression)
val signature = entity.signature
// is the best way to inspect the callee to see if onFailure, mapErr, etc are called on the returned Try?
if (signature.contains("onFailure") || signature.contains("mapErr")) return
val messageText = expression.calleeExpression?.text ?: expression.text
report(
CodeSmell(
issue,
entity,
message = "The call $messageText is returning a Try that does not handle the Throwable."
)
)
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
To answer your question,
Can you provide a snippet of what you're trying to achieve? |
Beta Was this translation helpful? Give feedback.
To answer your question,
visitCallExpression
is the correct function. You can take inspiration from theForbiddenMethodCall
rule:detekt/detekt-rules-style/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/style/ForbiddenMethodCall.kt
Lines 75 to 81 in c5f65a9