Skip to content

Commit 817a287

Browse files
committed
add test case with a proc macro fake_desugar_await
1 parent 99c7838 commit 817a287

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

tests/ui/auxiliary/proc_macro_attr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ pub fn rename_my_lifetimes(_args: TokenStream, input: TokenStream) -> TokenStrea
9595

9696
TokenStream::from(quote!(#item))
9797
}
98+
99+
#[proc_macro_attribute]
100+
pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream {
101+
let mut async_fn = syn::parse_macro_input!(input as syn::ItemFn);
102+
103+
for stmt in &mut async_fn.block.stmts {
104+
if let syn::Stmt::Expr(syn::Expr::Match(syn::ExprMatch { expr: scrutinee, .. }), _) = stmt {
105+
if let syn::Expr::Await(syn::ExprAwait { base, await_token, .. }) = scrutinee.as_mut() {
106+
let blc = quote_spanned!( await_token.span => {
107+
#[allow(clippy::let_and_return)]
108+
let __pinned = #base;
109+
__pinned
110+
});
111+
*scrutinee = parse_quote!(#blc);
112+
}
113+
}
114+
}
115+
116+
quote!(#async_fn).into()
117+
}

tests/ui/blocks_in_conditions.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::blocks_in_conditions)]
24
#![allow(unused, clippy::let_and_return, clippy::needless_if)]
35
#![warn(clippy::nonminimal_bool)]
@@ -85,4 +87,15 @@ fn block_in_match_expr(num: i32) -> i32 {
8587
}
8688
}
8789

90+
mod issue_12016 {
91+
#[proc_macro_attr::fake_desugar_await]
92+
pub async fn await_becomes_block() -> i32 {
93+
let res = await; match res {
94+
Some(1) => 2,
95+
Some(2) => 3,
96+
_ => 0,
97+
}
98+
}
99+
}
100+
88101
fn main() {}

tests/ui/blocks_in_conditions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::blocks_in_conditions)]
24
#![allow(unused, clippy::let_and_return, clippy::needless_if)]
35
#![warn(clippy::nonminimal_bool)]
@@ -85,4 +87,15 @@ fn block_in_match_expr(num: i32) -> i32 {
8587
}
8688
}
8789

90+
mod issue_12016 {
91+
#[proc_macro_attr::fake_desugar_await]
92+
pub async fn await_becomes_block() -> i32 {
93+
match Some(1).await {
94+
Some(1) => 2,
95+
Some(2) => 3,
96+
_ => 0,
97+
}
98+
}
99+
}
100+
88101
fn main() {}

tests/ui/blocks_in_conditions.stderr

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
2-
--> $DIR/blocks_in_conditions.rs:23:5
2+
--> $DIR/blocks_in_conditions.rs:25:5
33
|
44
LL | / if {
55
LL | |
@@ -20,13 +20,13 @@ LL ~ }; if res {
2020
|
2121

2222
error: omit braces around single expression condition
23-
--> $DIR/blocks_in_conditions.rs:35:8
23+
--> $DIR/blocks_in_conditions.rs:37:8
2424
|
2525
LL | if { true } { 6 } else { 10 }
2626
| ^^^^^^^^ help: try: `true`
2727

2828
error: this boolean expression can be simplified
29-
--> $DIR/blocks_in_conditions.rs:41:8
29+
--> $DIR/blocks_in_conditions.rs:43:8
3030
|
3131
LL | if true && x == 3 { 6 } else { 10 }
3232
| ^^^^^^^^^^^^^^ help: try: `x == 3`
@@ -35,7 +35,7 @@ LL | if true && x == 3 { 6 } else { 10 }
3535
= help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]`
3636

3737
error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
38-
--> $DIR/blocks_in_conditions.rs:68:5
38+
--> $DIR/blocks_in_conditions.rs:70:5
3939
|
4040
LL | / match {
4141
LL | |
@@ -53,5 +53,11 @@ LL + opt
5353
LL ~ }; match res {
5454
|
5555

56-
error: aborting due to 4 previous errors
56+
error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
57+
--> $DIR/blocks_in_conditions.rs:93:9
58+
|
59+
LL | match Some(1).await {
60+
| ^^^^^^^^^^^^^^^^^^^ help: try: `let res = await; match res`
61+
62+
error: aborting due to 5 previous errors
5763

0 commit comments

Comments
 (0)