Add new rule SleepInsteadOfDelay - #3335
Conversation
Added SleepInsteadOfDelay rule
Added entry to mention the new contribution in README.md
Codecov Report
@@ Coverage Diff @@
## master #3335 +/- ##
============================================
- Coverage 80.44% 80.25% -0.19%
- Complexity 2721 2727 +6
============================================
Files 445 446 +1
Lines 8250 8262 +12
Branches 1566 1568 +2
============================================
- Hits 6637 6631 -6
- Misses 772 785 +13
- Partials 841 846 +5
Continue to review full report at Codecov.
|
BraisGabin
left a comment
There was a problem hiding this comment.
It looks good :) just one more test and it's good to go for my end.
Added positive unit test for SleepInsteadOfDelay rule
| ?.resultingDescriptor | ||
| ?.fqNameOrNull() | ||
| ?.asString() | ||
| if (fqName == LAUNCH_COROUTINE_NAME) { |
There was a problem hiding this comment.
I think we should also check async here. This could be in a follow-up PR.
| } | ||
|
|
||
| override fun visitImportDirective(importDirective: KtImportDirective) { | ||
| sleepImported = importDirective.importedFqName?.asString() in IMPORT_PATHS |
There was a problem hiding this comment.
Is sleepImported needed at all? You're checking the fully qualified name of the Thread.sleep function below (line 92) so it should be irrelevant if sleep was imported or not.
There was a problem hiding this comment.
sleepImported is there to avoid having to search through all KtCallExpression descendents, given that there will only be dot-qualified expressions of Thread.sleep() if sleepImported is false.
There was a problem hiding this comment.
to avoid having to search through all KtCallExpression descendents
Are you trying to avoid this for performance reasons?
Added check for CoroutineScope.async(); Added @requiresTypeResolution for documentation
Removing active declaration for rule
Added test for sleep() invocation when Thread.sleep is imported and executed in a Coroutine block; Fixed bug where sleepImported could be set to false after visiting other import statements in a file
|
Suggestions for additional tests to increase code coverage would be welcome. |
|
Yes, basically. If Thread.sleep isn't imported, then there wouldn't be any
non dot-qualified expressions of the function, correct? In that case, the
rule could skip parsing through expressions that it wouldn't have an
expectation of finding the sleep() function.
Op wo 6 jan. 2021 21:44 schreef Nicola Corti <notifications@github.com>:
… ***@***.**** commented on this pull request.
------------------------------
In
detekt-rules-coroutines/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/coroutines/SleepInsteadOfDelay.kt
<#3335 (comment)>:
> + private var sleepImported = false
+
+ override val issue = Issue(
+ javaClass.simpleName,
+ Severity.Defect,
+ "Usage of Thread.sleep() in coroutines can potentially halt multiple coroutines at once.",
+ Debt.FIVE_MINS
+ )
+
+ override fun postVisit(root: KtFile) {
+ super.postVisit(root)
+ sleepImported = false
+ }
+
+ override fun visitImportDirective(importDirective: KtImportDirective) {
+ sleepImported = importDirective.importedFqName?.asString() in IMPORT_PATHS
to avoid having to search through all KtCallExpression descendents
Are you trying to avoid this for performance reasons?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3335 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC7SZ4IVJG4422X3XYQF6QDSYTDTRANCNFSM4VOIGSRA>
.
|
I would say that in this case the readability outweighs the performance benefits. It was not immediately clear to me why you were interested in the checking if I would simplify the private fun PsiElement.checkDescendants(message: String) {
forEachDescendantOfType<KtCallExpression> { it.verifyExpression(message) }
forEachDescendantOfType<KtDotQualifiedExpression> { it.verifyExpression(message) }
}You're anyway running |
Reducing granularity of checks for Thread.sleep() invocations, just checking on all KtCallExpression instances
|
It seems that although For me, this led to When I unknowingly tried to enable this with version |
That's expected as the documentation website follows the status of the current |
Added new SleepInsteadOfDelay rule for checking for whether
Thread.sleep()is being used in suspend functions and coroutines.