Skip to content

Commit

Permalink
Add #[must_use] annotation to expanded function (#26)
Browse files Browse the repository at this point in the history
This allows users to pick up situations where they've forgotten to await a function annotated with #[async_recursion].
  • Loading branch information
dcchut authored Jan 19, 2023
1 parent a32c277 commit 73509f2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ quote = { version = "1.0", default-features = false }
syn = { version = "1.0", features = ["full", "parsing", "printing", "proc-macro", "clone-impls"], default-features = false }

[dev-dependencies]
futures-executor = "0.3"
futures-executor = "0.3"
trybuild = "1.0"
1 change: 1 addition & 0 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl ToTokens for AsyncItem {
}

pub fn expand(item: &mut AsyncItem, args: &RecursionArgs) {
item.0.attrs.push(parse_quote!(#[must_use]));
transform_sig(&mut item.0.sig, args);
transform_block(&mut item.0.block);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}
10 changes: 10 additions & 0 deletions tests/ui/must_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(unused_must_use)]

use async_recursion::async_recursion;

#[async_recursion]
async fn apples(_: u16) {}

fn main() {
apples(3);
}
11 changes: 11 additions & 0 deletions tests/ui/must_use.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: unused return value of `apples` that must be used
--> tests/ui/must_use.rs:9:5
|
9 | apples(3);
| ^^^^^^^^^^
|
note: the lint level is defined here
--> tests/ui/must_use.rs:1:9
|
1 | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
6 changes: 6 additions & 0 deletions tests/ui/not_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use async_recursion::async_recursion;

#[async_recursion]
fn not_async() {}

fn main() {}
7 changes: 7 additions & 0 deletions tests/ui/not_async.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: expected an async function
--> tests/ui/not_async.rs:3:1
|
3 | #[async_recursion]
| ^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `async_recursion` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 73509f2

Please sign in to comment.