Create rule S7413 Await should be used for awaitable returns in async blocks and functions (#4752)
* Create rule S7413 * Update rule.adoc * Update metadata.json * Update rule.adoc * Update rule.adoc * Update metadata.json --------- Co-authored-by: sallaigy <sallaigy@users.noreply.github.com> Co-authored-by: Gyula Sallai <gyula.sallai@sonarsource.com>
This commit is contained in:
parent
d17e141be3
commit
cdb05a081b
2
rules/S7413/metadata.json
Normal file
2
rules/S7413/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7413/rust/metadata.json
Normal file
24
rules/S7413/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "Await should be used for awaitable returns in async blocks and functions",
|
||||||
|
"type": "BUG",
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "5min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"clippy"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Critical",
|
||||||
|
"ruleSpecification": "RSPEC-7413",
|
||||||
|
"sqKey": "S7413",
|
||||||
|
"scope": "All",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "unknown",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"RELIABILITY": "HIGH"
|
||||||
|
},
|
||||||
|
"attribute": "LOGICAL"
|
||||||
|
}
|
||||||
|
}
|
36
rules/S7413/rust/rule.adoc
Normal file
36
rules/S7413/rust/rule.adoc
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
== Why is this an issue?
|
||||||
|
When an async block or function returns a value that is itself awaitable (like a `Future`), it often indicates that the developer forgot to await that value. This creates a nested future that must be awaited twice to get the actual result, which is rarely the intended behavior. Missing an await can lead to unexpected behavior where async operations never actually execute, nested futures that require multiple awaits to resolve, hard-to-debug problems, potential deadlocks, or blocking in async contexts.
|
||||||
|
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
async fn foo() {}
|
||||||
|
|
||||||
|
fn bar() {
|
||||||
|
let x = async {
|
||||||
|
foo() // Noncompliant: returns a future that needs to be awaited
|
||||||
|
};
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
async fn foo() {}
|
||||||
|
|
||||||
|
fn bar() {
|
||||||
|
let x = async {
|
||||||
|
foo().await // Properly awaits the inner future
|
||||||
|
};
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#async_yields_async
|
Loading…
x
Reference in New Issue
Block a user