-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SuspendFunSwallowedCancellation rule #5666
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice, looks pretty advanced, looking at the issue description I didn't expect this complexity.
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Outdated
Show resolved
Hide resolved
.../test/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatchingSpec.kt
Outdated
Show resolved
Hide resolved
.../test/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatchingSpec.kt
Outdated
Show resolved
Hide resolved
.../test/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatchingSpec.kt
Outdated
Show resolved
Hide resolved
.../test/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatchingSpec.kt
Outdated
Show resolved
Hide resolved
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Outdated
Show resolved
Hide resolved
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Outdated
Show resolved
Hide resolved
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Outdated
Show resolved
Hide resolved
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Outdated
Show resolved
Hide resolved
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Outdated
Show resolved
Hide resolved
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Fixed
Show fixed
Hide fixed
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Fixed
Show fixed
Hide fixed
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Fixed
Show fixed
Hide fixed
.../src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunInsideRunCatching.kt
Fixed
Show fixed
Hide fixed
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
...t/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellationSpec.kt
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like the code is getting shorter, but definitely simpler, nice one!
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Show resolved
Hide resolved
.../main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellation.kt
Outdated
Show resolved
Hide resolved
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. |
...t/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellationSpec.kt
Outdated
Show resolved
Hide resolved
...t/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellationSpec.kt
Show resolved
Hide resolved
...t/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SuspendFunSwallowedCancellationSpec.kt
Outdated
Show resolved
Hide resolved
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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 fun
inside arunCatching
block. Currently, I am finding each suspension point(suspend fun
) inside therunCatching
block and then for each suspension point trying to backpropagate if I can reach back to the originalrunCatching
block 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
runCatching
block)noinline
orcrossinline
)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
runCatching
block(I am not checking againstfqName
as there could be multiple nestedrunCatching
and 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 fun
from 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
noinline
orcrossinline
(This helped in preventing the backpropagation from the suspension point)