Add SuspendFunSwallowedCancellation rule#5666
Conversation
TWiStErRob
left a comment
There was a problem hiding this comment.
Really nice, looks pretty advanced, looking at the issue description I didn't expect this complexity.
TWiStErRob
left a comment
There was a problem hiding this comment.
Feels like the code is getting shorter, but definitely simpler, nice one!
Thanks |
Codecov Report
@@ Coverage Diff @@
## main #5666 +/- ##
============================================
- Coverage 84.59% 84.58% -0.01%
- Complexity 3790 3816 +26
============================================
Files 546 547 +1
Lines 12918 12969 +51
Branches 2268 2286 +18
============================================
+ Hits 10928 10970 +42
- Misses 861 862 +1
- Partials 1129 1137 +8
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
|
I was out for some time and there are a lot of comments on these PR. Is there any blockers to merge this? Can I help to unblock something? To me the tests feel a bit too long but this rule is a clear win for our users so we can improve that in later PRs. I mean, this implements just "half" issue. The |
|
Hi @BraisGabin |
|
@BraisGabin note: unresolved conversations are unresolved, hence the name ;) |
Add suggestion from code review Co-authored-by: Róbert Papp <papp.robert.s@gmail.com>
Rename rule to SuspendFunSwallowedCancellation Add new TCs
Check shouldPropagate inside while checking for loop Simplify if else block
Make the local function Early return of the declaration of the variable
Report violating runCatching block instead of suspend call Assert location in tests
Add TCs for suspend fun noinline and crossinline lambda
Hi @TWiStErRob can you check once again? I have added the TC as well for your case and it is passing |
|
I think it's sufficiently covered with tests, so let's release it and see if false positives/negatives come up. I would add a note in the issue description something like: I know this is the default expected behavior, but in the case of this rule, it's even more important to not just "suppress it in my case", but discuss usage patterns we didn't think of. |
Merging as we want this included in 1.23 👍 |
Fixes #5468
More details:
This PR tries to detekt, all the
suspend funinside arunCatchingblock. Currently, I am finding each suspension point(suspend fun) inside therunCatchingblock and then for each suspension point trying to backpropagate if I can reach back to the originalrunCatchingblock or not.Cases, where I am not able to reach, are as follows (these cases will not be reported a code smell for the current
runCatchingblock)noinlineorcrossinline)If any of the above points gets encountered, I break the backpropagation and then check if the returned function descriptor is the same as the current
runCatchingblock(I am not checking againstfqNameas there could be multiple nestedrunCatchingand then deeper suspension point will be reported against all the parentrunCatching)I have created this draft PR for early feedback and suggestion on improving the propagation around TCs. Although I agree that I am propagation a
runCatching..suspend funfrom top to bottom then bottom to top, I want to know if should I optimise this(do this in single propagation(This will require custom propagation of child or some other logic)) or is it okay. I'm fine either wayUpdate:
Now in the commit, I have simplified the traversal logic and only going inside a function for traversal when that function is inline or value parameter of the function is not marked
noinlineorcrossinline(This helped in preventing the backpropagation from the suspension point)