-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* AsyncAndSyncRule ### What's done: Implemented rule, added tests and documentation
- Loading branch information
Showing
10 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/AsyncAndSyncRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.cqfn.diktat.ruleset.rules | ||
|
||
import org.cqfn.diktat.common.config.rules.RulesConfig | ||
import org.cqfn.diktat.ruleset.constants.EmitType | ||
import org.cqfn.diktat.ruleset.constants.Warnings.RUN_BLOCKING_INSIDE_ASYNC | ||
import org.cqfn.diktat.ruleset.utils.hasChildOfType | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.ElementType.FUN | ||
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_ARGUMENT | ||
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION | ||
import com.pinterest.ktlint.core.ast.parent | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.psi.KtFunction | ||
import org.jetbrains.kotlin.psi.psiUtil.hasSuspendModifier | ||
|
||
/** | ||
* This rule finds if using runBlocking in asynchronous code | ||
*/ | ||
class AsyncAndSyncRule(private val configRules: List<RulesConfig>) : Rule("sync-in-async") { | ||
private val asyncList = listOf("async", "launch") | ||
private var isFixMode: Boolean = false | ||
private lateinit var emitWarn: EmitType | ||
|
||
override fun visit(node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: EmitType) { | ||
emitWarn = emit | ||
isFixMode = autoCorrect | ||
|
||
if (node.isRunBlocking()) { | ||
checkRunBlocking(node) | ||
} | ||
} | ||
|
||
private fun checkRunBlocking(node: ASTNode) { | ||
node.parent({it.isAsync() || it.isSuspend()})?.let { | ||
RUN_BLOCKING_INSIDE_ASYNC.warn(configRules, emitWarn, isFixMode, node.text, node.startOffset, node) | ||
} | ||
} | ||
|
||
private fun ASTNode.isAsync() = this.elementType == CALL_EXPRESSION && this.findChildByType(REFERENCE_EXPRESSION)?.text in asyncList | ||
|
||
private fun ASTNode.isSuspend() = this.elementType == FUN && (this.psi as KtFunction).modifierList?.hasSuspendModifier() ?: false | ||
|
||
private fun ASTNode.isRunBlocking() = this.elementType == REFERENCE_EXPRESSION && this.text == "runBlocking" && this.treeParent.hasChildOfType(LAMBDA_ARGUMENT) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter5/AsyncAndSyncRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.cqfn.diktat.ruleset.chapter5 | ||
|
||
import org.cqfn.diktat.ruleset.constants.Warnings.RUN_BLOCKING_INSIDE_ASYNC | ||
import org.cqfn.diktat.ruleset.rules.AsyncAndSyncRule | ||
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID | ||
import org.cqfn.diktat.util.LintTestBase | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import generated.WarningNames | ||
import org.junit.jupiter.api.Tag | ||
import org.junit.jupiter.api.Test | ||
|
||
class AsyncAndSyncRuleTest : LintTestBase(::AsyncAndSyncRule) { | ||
private val ruleId = "$DIKTAT_RULE_SET_ID:sync-in-async" | ||
|
||
@Test | ||
@Tag(WarningNames.RUN_BLOCKING_INSIDE_ASYNC) | ||
fun `test wrong case`() { | ||
lintMethod( | ||
""" | ||
|fun foo() { | ||
| GlobalScope.launch { | ||
| c.addAndGet(i) | ||
| } | ||
| | ||
| GlobalScope.async { | ||
| n | ||
| } | ||
| | ||
| GlobalScope.async { | ||
| runBlocking { | ||
| | ||
| } | ||
| } | ||
|} | ||
| | ||
|suspend fun foo() { | ||
| runBlocking { | ||
| delay(2000) | ||
| } | ||
|} | ||
| | ||
""".trimMargin(), | ||
LintError(11, 8, ruleId, "${RUN_BLOCKING_INSIDE_ASYNC.warnText()} runBlocking", false), | ||
LintError(18, 4, ruleId, "${RUN_BLOCKING_INSIDE_ASYNC.warnText()} runBlocking", false) | ||
) | ||
} | ||
|
||
@Test | ||
@Tag(WarningNames.RUN_BLOCKING_INSIDE_ASYNC) | ||
fun `test dot qualified expression case`() { | ||
lintMethod( | ||
""" | ||
|fun foo() { | ||
| GlobalScope.async { | ||
| node.runBlocking() | ||
| runBlocking { | ||
| n++ | ||
| } | ||
| } | ||
|} | ||
| | ||
|fun goo() { | ||
| runBlocking { | ||
| GlobalScope.async { | ||
| n++ | ||
| } | ||
| } | ||
|} | ||
""".trimMargin(), | ||
LintError(4, 8, ruleId, "${RUN_BLOCKING_INSIDE_ASYNC.warnText()} runBlocking", false) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
373b609
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.
I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:
Please, copy and paste this stack trace to GitHub: