Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 92537a0

Browse files
committed
add test case with a proc macro fake_desugar_await
1 parent 28443e6 commit 92537a0

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
@@ -125,3 +125,23 @@ pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
125125
}
126126
.into()
127127
}
128+
129+
#[proc_macro_attribute]
130+
pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream {
131+
let mut async_fn = syn::parse_macro_input!(input as syn::ItemFn);
132+
133+
for stmt in &mut async_fn.block.stmts {
134+
if let syn::Stmt::Expr(syn::Expr::Match(syn::ExprMatch { expr: scrutinee, .. }), _) = stmt {
135+
if let syn::Expr::Await(syn::ExprAwait { base, await_token, .. }) = scrutinee.as_mut() {
136+
let blc = quote_spanned!( await_token.span => {
137+
#[allow(clippy::let_and_return)]
138+
let __pinned = #base;
139+
__pinned
140+
});
141+
*scrutinee = parse_quote!(#blc);
142+
}
143+
}
144+
}
145+
146+
quote!(#async_fn).into()
147+
}

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)]
@@ -99,4 +101,15 @@ fn issue_12162() {
99101
}
100102
}
101103

104+
mod issue_12016 {
105+
#[proc_macro_attr::fake_desugar_await]
106+
pub async fn await_becomes_block() -> i32 {
107+
let res = await; match res {
108+
Some(1) => 2,
109+
Some(2) => 3,
110+
_ => 0,
111+
}
112+
}
113+
}
114+
102115
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)]
@@ -99,4 +101,15 @@ fn issue_12162() {
99101
}
100102
}
101103

104+
mod issue_12016 {
105+
#[proc_macro_attr::fake_desugar_await]
106+
pub async fn await_becomes_block() -> i32 {
107+
match Some(1).await {
108+
Some(1) => 2,
109+
Some(2) => 3,
110+
_ => 0,
111+
}
112+
}
113+
}
114+
102115
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:107: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)