forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement diagnostic for
await
outside of async
- Loading branch information
1 parent
6da26c1
commit 3e809f8
Showing
6 changed files
with
187 additions
and
17 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
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
101 changes: 101 additions & 0 deletions
101
src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/await_outside_of_async.rs
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,101 @@ | ||
use crate::{adjusted_display_range, Diagnostic, DiagnosticsContext}; | ||
|
||
// Diagnostic: await-outside-of-async | ||
// | ||
// This diagnostic is triggered if the `await` keyword is used outside of an async function or block | ||
pub(crate) fn await_outside_of_async( | ||
ctx: &DiagnosticsContext<'_>, | ||
d: &hir::AwaitOutsideOfAsync, | ||
) -> Diagnostic { | ||
let display_range = | ||
adjusted_display_range(ctx, d.node, &|node| Some(node.await_token()?.text_range())); | ||
Diagnostic::new( | ||
crate::DiagnosticCode::RustcHardError("E0728"), | ||
format!("`await` is used inside {}, which is not an `async` context", d.location), | ||
display_range, | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::check_diagnostics; | ||
|
||
#[test] | ||
fn await_inside_non_async_fn() { | ||
check_diagnostics( | ||
r#" | ||
async fn foo() {} | ||
fn bar() { | ||
foo().await; | ||
//^^^^^ error: `await` is used inside non-async function, which is not an `async` context | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn await_inside_async_fn() { | ||
check_diagnostics( | ||
r#" | ||
async fn foo() {} | ||
async fn bar() { | ||
foo().await; | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn await_inside_closure() { | ||
check_diagnostics( | ||
r#" | ||
async fn foo() {} | ||
async fn bar() { | ||
let _a = || { foo().await }; | ||
//^^^^^ error: `await` is used inside non-async closure, which is not an `async` context | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn await_inside_async_block() { | ||
check_diagnostics( | ||
r#" | ||
async fn foo() {} | ||
fn bar() { | ||
let _a = async { foo().await }; | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn await_in_complex_context() { | ||
check_diagnostics( | ||
r#" | ||
async fn foo() {} | ||
fn bar() { | ||
async fn baz() { | ||
let a = foo().await; | ||
} | ||
let x = || { | ||
let y = async { | ||
baz().await; | ||
let z = || { | ||
baz().await; | ||
//^^^^^ error: `await` is used inside non-async closure, which is not an `async` context | ||
}; | ||
}; | ||
}; | ||
} | ||
"#, | ||
); | ||
} | ||
} |
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