about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJ-ZhengLi <lizheng135@huawei.com>2023-12-29 10:15:45 +0800
committerJ-ZhengLi <lizheng135@huawei.com>2024-02-10 22:26:50 +0800
commit92537a0e5b73629b7c25061ccff6dc255e53c6b6 (patch)
tree19f26b08ffd8f5603bbf435d8afeab6c0c824396
parent28443e63fb633a5ed00a49e8df591a956d77122c (diff)
downloadrust-92537a0e5b73629b7c25061ccff6dc255e53c6b6.tar.gz
rust-92537a0e5b73629b7c25061ccff6dc255e53c6b6.zip
add test case with a proc macro `fake_desugar_await`
-rw-r--r--tests/ui/auxiliary/proc_macro_attr.rs20
-rw-r--r--tests/ui/blocks_in_conditions.fixed13
-rw-r--r--tests/ui/blocks_in_conditions.rs13
-rw-r--r--tests/ui/blocks_in_conditions.stderr16
4 files changed, 57 insertions, 5 deletions
diff --git a/tests/ui/auxiliary/proc_macro_attr.rs b/tests/ui/auxiliary/proc_macro_attr.rs
index ac544737099..75f7a20f961 100644
--- a/tests/ui/auxiliary/proc_macro_attr.rs
+++ b/tests/ui/auxiliary/proc_macro_attr.rs
@@ -125,3 +125,23 @@ pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
     }
     .into()
 }
+
+#[proc_macro_attribute]
+pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream {
+    let mut async_fn = syn::parse_macro_input!(input as syn::ItemFn);
+
+    for stmt in &mut async_fn.block.stmts {
+        if let syn::Stmt::Expr(syn::Expr::Match(syn::ExprMatch { expr: scrutinee, .. }), _) = stmt {
+            if let syn::Expr::Await(syn::ExprAwait { base, await_token, .. }) = scrutinee.as_mut() {
+                let blc = quote_spanned!( await_token.span => {
+                    #[allow(clippy::let_and_return)]
+                    let __pinned = #base;
+                    __pinned
+                });
+                *scrutinee = parse_quote!(#blc);
+            }
+        }
+    }
+
+    quote!(#async_fn).into()
+}
diff --git a/tests/ui/blocks_in_conditions.fixed b/tests/ui/blocks_in_conditions.fixed
index efef60567a9..ac1ad68ffc2 100644
--- a/tests/ui/blocks_in_conditions.fixed
+++ b/tests/ui/blocks_in_conditions.fixed
@@ -1,3 +1,5 @@
+//@aux-build:proc_macro_attr.rs
+
 #![warn(clippy::blocks_in_conditions)]
 #![allow(unused, clippy::let_and_return, clippy::needless_if)]
 #![warn(clippy::nonminimal_bool)]
@@ -99,4 +101,15 @@ fn issue_12162() {
     }
 }
 
+mod issue_12016 {
+    #[proc_macro_attr::fake_desugar_await]
+    pub async fn await_becomes_block() -> i32 {
+        let res = await; match res {
+            Some(1) => 2,
+            Some(2) => 3,
+            _ => 0,
+        }
+    }
+}
+
 fn main() {}
diff --git a/tests/ui/blocks_in_conditions.rs b/tests/ui/blocks_in_conditions.rs
index 8bd8a939eb1..e72daaa910d 100644
--- a/tests/ui/blocks_in_conditions.rs
+++ b/tests/ui/blocks_in_conditions.rs
@@ -1,3 +1,5 @@
+//@aux-build:proc_macro_attr.rs
+
 #![warn(clippy::blocks_in_conditions)]
 #![allow(unused, clippy::let_and_return, clippy::needless_if)]
 #![warn(clippy::nonminimal_bool)]
@@ -99,4 +101,15 @@ fn issue_12162() {
     }
 }
 
+mod issue_12016 {
+    #[proc_macro_attr::fake_desugar_await]
+    pub async fn await_becomes_block() -> i32 {
+        match Some(1).await {
+            Some(1) => 2,
+            Some(2) => 3,
+            _ => 0,
+        }
+    }
+}
+
 fn main() {}
diff --git a/tests/ui/blocks_in_conditions.stderr b/tests/ui/blocks_in_conditions.stderr
index b00fe2f632c..cb2f6da3516 100644
--- a/tests/ui/blocks_in_conditions.stderr
+++ b/tests/ui/blocks_in_conditions.stderr
@@ -1,5 +1,5 @@
 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`
-  --> $DIR/blocks_in_conditions.rs:23:5
+  --> $DIR/blocks_in_conditions.rs:25:5
    |
 LL | /     if {
 LL | |
@@ -20,13 +20,13 @@ LL ~     }; if res {
    |
 
 error: omit braces around single expression condition
-  --> $DIR/blocks_in_conditions.rs:35:8
+  --> $DIR/blocks_in_conditions.rs:37:8
    |
 LL |     if { true } { 6 } else { 10 }
    |        ^^^^^^^^ help: try: `true`
 
 error: this boolean expression can be simplified
-  --> $DIR/blocks_in_conditions.rs:41:8
+  --> $DIR/blocks_in_conditions.rs:43:8
    |
 LL |     if true && x == 3 { 6 } else { 10 }
    |        ^^^^^^^^^^^^^^ help: try: `x == 3`
@@ -35,7 +35,7 @@ LL |     if true && x == 3 { 6 } else { 10 }
    = help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]`
 
 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`
-  --> $DIR/blocks_in_conditions.rs:68:5
+  --> $DIR/blocks_in_conditions.rs:70:5
    |
 LL | /     match {
 LL | |
@@ -53,5 +53,11 @@ LL +         opt
 LL ~     }; match res {
    |
 
-error: aborting due to 4 previous errors
+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`
+  --> $DIR/blocks_in_conditions.rs:107:9
+   |
+LL |         match Some(1).await {
+   |         ^^^^^^^^^^^^^^^^^^^ help: try: `let res = await; match res`
+
+error: aborting due to 5 previous errors