https://kotlinlang.org logo
#detekt
Title
# detekt
p

Paolo Rotolo

12/06/2023, 12:11 PM
Hey! Sorry, I need a bit of help with this PR: https://github.com/detekt/detekt/pull/6692 My idea is: - Take a function (f) annotated with
@Test
(org.junit.Test) - Check if there are coroutine launches outside runTest block - Collect all the functions of the same codebase called in (f) and perform the same check on them and so on. I’m new to PSI APIs and I didn’t find a lot of documentation online, but I’m very curious to learn more. I think I’ve got the first two steps in my draft PR, but I’ve no idea which API to use to retrieve all the function calls in the scope of the
@Test
function that could launch a coroutine internally. I’ve also explored the inverse path: visiting all the functions using Visitor’s
visitNamedFunction
and finding if there’s a
@Test
function parent, but it would be a less optimized solution I guess?
👀 1
g

gammax

12/07/2023, 12:18 PM
I’m new to PSI APIs and I didn’t find a lot of documentation online
That’s sadly true for all the PSI related questions 😞
🫠 1
The best would be to search for similar rules inside the Detekt codebase that achieve a similar result
p

Paolo Rotolo

12/07/2023, 1:33 PM
yeah, I've found some rules that do the inverse (from function, take parent)... I'll try to investigate more later 😕
After experimenting with PSI, I think I've found a way of solving it with:
Copy code
function.collectDescendantsOfType<KtExpression>().mapNotNull {
                it.getResolvedCall(bindingContext)
                    ?.resultingDescriptor
                    ?.source
                    ?.getPsi() as? KtNamedFunction 
        }
Will update the PR soon
❤️ 1
a

Atul Gupta

12/12/2023, 9:04 PM
I would try to traverse in and see if any function is calls a function which launches coroutine(maybe based on signature) something like below
Copy code
expression.anyDescendantOfType<KtExpression>(::shouldTraverseInside) { descendant ->
    descendant.hasSuspendCalls()
}.ifTrue { report(expression) }
You can refer
SuspendFunSwallowedCancellation
which finds the call to
runCatching
then checks each child function for possible violations
👍 1
6 Views