We introduced a small `Result<T>` extension...
# detekt
d
We introduced a small
Result<T>
extension called
onFailureOrRethrowCancellation
which exactly does what the name suggests, allowing the handling of anything that is not a
CancellationException
. Now of course Detekt does not know about this method, so when it checks our
runCatching
code and sees a suspending method call, it still flags
SuspendFunSwallowedCancellation
. Would there be any way to let Detekt “learn” that this method “does the right thing” (TM), e.g. by annotating it somehow?
b
You could reimplement the rule (copy it in a your own custom rule set) with that change and disable the default one.
On my project we took a different solution:
Copy code
inline fun R runCatchingSuspend(block: () -> R): ResultR {
    @Suppress("SuspendFunSwallowedCancellation")
    return runCatching(block)
        .onFailure { if (it is CancellationException) throw it }
}
And we don’t use
runCatching
but
runCatchingSuspend
. (The name isn’t good). And detekt is completely fine with this.
👍 1