Skip to content
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 SuspendFunWithCoroutineScopeReceiver Rule #4616

Merged
merged 1 commit into from Mar 23, 2022
Merged

Add SuspendFunWithCoroutineScopeReceiver Rule #4616

merged 1 commit into from Mar 23, 2022

Conversation

hfhbd
Copy link
Contributor

@hfhbd hfhbd commented Mar 7, 2022

Fixes #4587

Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

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

Really nice PR! :)

To make CI happy you need to run ./gradlew generateDocumentation and commit the changes that it will add in the default-detekt-config file.

@hfhbd
Copy link
Contributor Author

hfhbd commented Mar 7, 2022

Ah, there is the default configuration. I've already searched for it :D

Copy link
Member

@BraisGabin BraisGabin left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the contribution!

@BraisGabin BraisGabin added this to the 1.20.0 milestone Mar 7, 2022
@BraisGabin BraisGabin added the notable changes Marker for notable changes in the changelog label Mar 7, 2022
@hfhbd
Copy link
Contributor Author

hfhbd commented Mar 7, 2022

I forgot the embedded Kotlin compiler provided by Gradle uses a different language API than the actual compiler executing the tests. So you can't use Duration, an inline class with compile-test-snippets=true.

@cortinico cortinico changed the title Add SuspendFunWithCoroutineScopeReceiverRule Add SuspendFunWithCoroutineScopeReceiver Rule Mar 10, 2022
@BraisGabin
Copy link
Member

I just found this on the project I work: fun foo(block: suspend TestCoroutineScope.() -> Unit) {}. This is basically the same issue. Should we handle it in this rule too?

@hfhbd
Copy link
Contributor Author

hfhbd commented Mar 20, 2022

@BraisGabin Sure, added

@codecov
Copy link

codecov bot commented Mar 20, 2022

Codecov Report

Merging #4616 (d8b4bd8) into main (481b3b2) will increase coverage by 84.54%.
The diff coverage is 84.21%.

@@             Coverage Diff             @@
##             main    #4616       +/-   ##
===========================================
+ Coverage        0   84.54%   +84.54%     
- Complexity      0     3381     +3381     
===========================================
  Files           0      483      +483     
  Lines           0    11242    +11242     
  Branches        0     2050     +2050     
===========================================
+ Hits            0     9504     +9504     
- Misses          0      698      +698     
- Partials        0     1040     +1040     
Impacted Files Coverage Δ
...coroutines/SuspendFunWithCoroutineScopeReceiver.kt 83.33% <83.33%> (ø)
...osch/detekt/rules/coroutines/CoroutinesProvider.kt 100.00% <100.00%> (ø)
...b/arturbosch/detekt/rules/naming/FunctionNaming.kt 100.00% <0.00%> (ø)
...detekt/rules/exceptions/ThrowingExceptionInMain.kt 90.90% <0.00%> (ø)
...itlab/arturbosch/detekt/rules/naming/EnumNaming.kt 93.75% <0.00%> (ø)
...tyle/DestructuringDeclarationWithTooManyEntries.kt 100.00% <0.00%> (ø)
.../gitlab/arturbosch/detekt/rules/ThrowExtensions.kt 50.00% <0.00%> (ø)
...ab/arturbosch/detekt/cli/DetektProgressListener.kt 90.00% <0.00%> (ø)
...lab/arturbosch/detekt/formatting/FormattingRule.kt 98.07% <0.00%> (ø)
...lab/arturbosch/detekt/rules/style/UnusedImports.kt 88.60% <0.00%> (ø)
... and 474 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 481b3b2...d8b4bd8. Read the comment docs.

@BraisGabin
Copy link
Member

Oh, it seems that #4630 is making this PR to fail now. I suppose that they are just the indentation of """.

@hfhbd
Copy link
Contributor Author

hfhbd commented Mar 20, 2022

Yeah, fixed it.

@hfhbd
Copy link
Contributor Author

hfhbd commented Mar 21, 2022

@BraisGabin What about changing compile-test-snippets to true by default, to run this check with gradlew build too?

@BraisGabin
Copy link
Member

This is kind of a pain for sure. If we have enable by default we are testing the test code every time. And this task is slow. For that reason we don't have it enable by default.

@cortinico cortinico merged commit 088eae4 into detekt:main Mar 23, 2022
chao2zhang pushed a commit that referenced this pull request Apr 8, 2022
Co-authored-by: hfhbd <hfhbd@users.noreply.github.com>
@hfhbd
Copy link
Contributor Author

hfhbd commented Apr 17, 2022

@BraisGabin BTW are you sure about flagging lambdas too? In IntelliJ, only the function with the receiver is marked, but I don't know if this is a bug of IntelliJ...

import kotlinx.coroutines.*
import kotlin.time.Duration.Companion.seconds

suspend fun main() {
    coroutineScope {
        receiver()
    }
    block {
        launch {

        }
    }
}

suspend fun CoroutineScope.receiver(block: suspend CoroutineScope.() -> Unit) { // Ambiguous coroutineContext due to CoroutineScope receiver of suspend function
    delay(1.seconds)
    launch { // Ambiguous coroutineContext due to CoroutineScope receiver of suspend function
        delay(1.seconds)
        block()
    }
}

suspend fun block(block: suspend CoroutineScope.() -> Unit) {
    delay(1.seconds)
    coroutineScope {
        delay(1.seconds)
        block()
    }
} 

@BraisGabin
Copy link
Member

block {
  launch { } // Which scope am I using the one from suspend or the receiver? 
}

I think that the issue is the same. But can you open an issue to intellij to confirm that is a missing part on their end?

@hfhbd
Copy link
Contributor Author

hfhbd commented Apr 18, 2022

Sure.
CoroutineScope: the inner one wins
coroutineContext: this is the problem, because the outer one wins.

My concern about lambdas is its usage inside coroutine-core itself.

@hfhbd
Copy link
Contributor Author

hfhbd commented Apr 18, 2022

@BraisGabin
Copy link
Member

We can wait to see what they say in the new issue. But for me the issue is the same. In the body of those lambdas is confusing what coroutineContext mean. And that's the main reason to use forbid it in funs.

@hfhbd
Copy link
Contributor Author

hfhbd commented Apr 22, 2022

@BraisGabin Got an interesting answer. https://youtrack.jetbrains.com/issue/KT-52042/Clarifying-suspend-lambda-and-function-with-coroutineScope-as-pa#focus=Comments-27-6029021.0-0
So I would revert marking the lambda parameter too and focus on suspend fun CoroutineScope.foo only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
notable changes Marker for notable changes in the changelog rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New rule: suspend fun with CoroutineScope as Receiver
3 participants