Skip to content

Improve SuspendFunSwallowedCancellation#9208

Open
matejdro wants to merge 10 commits into
detekt:mainfrom
matejdro:improved-fun-swallowed-cancellation
Open

Improve SuspendFunSwallowedCancellation#9208
matejdro wants to merge 10 commits into
detekt:mainfrom
matejdro:improved-fun-swallowed-cancellation

Conversation

@matejdro
Copy link
Copy Markdown
Contributor

PR does three things:

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 36.66667% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.74%. Comparing base (d2e7e8e) to head (c4b26ef).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
...ules/coroutines/SuspendFunSwallowedCancellation.kt 34.78% 3 Missing and 12 partials ⚠️
...kt/rules/exceptions/InstanceOfCheckForException.kt 42.85% 1 Missing and 3 partials ⚠️

❌ Your patch check has failed because the patch coverage (36.66%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9208      +/-   ##
============================================
- Coverage     84.92%   84.74%   -0.19%     
- Complexity     4427     4439      +12     
============================================
  Files           570      569       -1     
  Lines         12299    12311      +12     
  Branches       2694     2720      +26     
============================================
- Hits          10445    10433      -12     
- Misses          683      692       +9     
- Partials       1171     1186      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@matejdro matejdro changed the title Improved fun swallowed cancellation Improved SuspendFunSwallowedCancellation Mar 31, 2026
@matejdro matejdro changed the title Improved SuspendFunSwallowedCancellation Improve SuspendFunSwallowedCancellation Mar 31, 2026
@matejdro
Copy link
Copy Markdown
Contributor Author

Hm I just noticed that this case was not covered:

runCatching {
    suspendCall()
}.onFailure { error ->
    if (error is CancellationException) currentCoroutineContext().ensureActive()
}.getOrNull()

I wonder, should we cover it? It seems pretty complex as runCatching returns a Result that can be passed anywhere and then onFailure called anywhere else. Or do we just support this simple case, where onFailure is called immediately?

val exceptionType = typeReference?.type

val cancellationExceptionType = findTypeAlias(CANCELLATION_EXCEPTION_CLASS_ID)?.defaultType
?: error("Cancellation exception type not found. Are coroutines on the classpath?")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do this. Someone could has 4 modules in their project, enables the coroutine rule set at all of them but only use coroutines in 3 of them. The forth would always fail.

If we can't find it we should assume that everything is correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. Fixed.

}

@Test
fun `does not report when using ensureActive approach to handle cancellation`() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you merge this test and the following one using @ParametrizedTest? They are really similar.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


val ensureActiveExpression = ifAtStart.then as? KtDotQualifiedExpression ?: return false

return (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary (?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it's slightly cleaner that way, but not by much. Remove.d


override fun visitTryExpression(expression: KtTryExpression) {
super.visitTryExpression(expression)
analyze(expression) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is a bad idea. analyze is really slow. Can we reduce its scope?

Copy link
Copy Markdown
Contributor Author

@matejdro matejdro Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved it to the nested function, but I'm wondering how is analyze cached? Because it's called anyway in the isSuspend() a little further down and now for every catch clause (inside for loop).

Is analysis cached or is every analyze() call slow? If later, it might be more efficient to just call it once on the top level, rather than analyzing multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive on SuspendFunSwallowedCancellation

3 participants