]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/redundant_async_block.fixed
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / redundant_async_block.fixed
1 #![allow(unused, clippy::manual_async_fn)]
2 #![warn(clippy::redundant_async_block)]
3
4 use std::future::Future;
5
6 async fn func1(n: usize) -> usize {
7 n + 1
8 }
9
10 async fn func2() -> String {
11 let s = String::from("some string");
12 let f = async { (*s).to_owned() };
13 let x = f;
14 x.await
15 }
16
17 fn main() {
18 let fut1 = async { 17 };
19 // Lint
20 let fut2 = fut1;
21
22 let fut1 = async { 25 };
23 // Lint
24 let fut2 = fut1;
25
26 // Lint
27 let fut = async { 42 };
28
29 // Do not lint: not a single expression
30 let fut = async {
31 func1(10).await;
32 func2().await
33 };
34
35 // Do not lint: expression contains `.await`
36 let fut = async { func1(func2().await.len()).await };
37 }
38
39 #[allow(clippy::let_and_return)]
40 fn capture_local() -> impl Future<Output = i32> {
41 let fut = async { 17 };
42 // Lint
43 fut
44 }
45
46 fn capture_local_closure(s: &str) -> impl Future<Output = &str> {
47 let f = move || std::future::ready(s);
48 // Do not lint: `f` would not live long enough
49 async move { f().await }
50 }
51
52 #[allow(clippy::let_and_return)]
53 fn capture_arg(s: &str) -> impl Future<Output = &str> {
54 let fut = async move { s };
55 // Lint
56 fut
57 }
58
59 fn capture_future_arg<T>(f: impl Future<Output = T>) -> impl Future<Output = T> {
60 // Lint
61 f
62 }
63
64 fn capture_func_result<FN, F, T>(f: FN) -> impl Future<Output = T>
65 where
66 F: Future<Output = T>,
67 FN: FnOnce() -> F,
68 {
69 // Do not lint, as f() would be evaluated prematurely
70 async { f().await }
71 }
72
73 fn double_future(f: impl Future<Output = impl Future<Output = u32>>) -> impl Future<Output = u32> {
74 // Do not lint, we will get a `.await` outside a `.async`
75 async { f.await.await }
76 }
77
78 fn await_in_async<F, R>(f: F) -> impl Future<Output = u32>
79 where
80 F: FnOnce() -> R,
81 R: Future<Output = u32>,
82 {
83 // Lint
84 async { f().await + 1 }
85 }
86
87 #[derive(Debug, Clone)]
88 struct F {}
89
90 impl F {
91 async fn run(&self) {}
92 }
93
94 pub async fn run() {
95 let f = F {};
96 let c = f.clone();
97 // Do not lint: `c` would not live long enough
98 spawn(async move { c.run().await });
99 let _f = f;
100 }
101
102 fn spawn<F: Future + 'static>(_: F) {}
103
104 async fn work(_: &str) {}
105
106 fn capture() {
107 let val = "Hello World".to_owned();
108 // Do not lint: `val` would not live long enough
109 spawn(async { work(&{ val }).await });
110 }
111
112 fn await_from_macro() -> impl Future<Output = u32> {
113 macro_rules! mac {
114 ($e:expr) => {
115 $e.await
116 };
117 }
118 // Do not lint: the macro may change in the future
119 // or return different things depending on its argument
120 async { mac!(async { 42 }) }
121 }
122
123 fn async_expr_from_macro() -> impl Future<Output = u32> {
124 macro_rules! mac {
125 () => {
126 async { 42 }
127 };
128 }
129 // Do not lint: the macro may change in the future
130 async { mac!().await }
131 }
132
133 fn async_expr_from_macro_deep() -> impl Future<Output = u32> {
134 macro_rules! mac {
135 () => {
136 async { 42 }
137 };
138 }
139 // Do not lint: the macro may change in the future
140 async { ({ mac!() }).await }
141 }
142
143 fn all_from_macro() -> impl Future<Output = u32> {
144 macro_rules! mac {
145 () => {
146 // Lint
147 async { 42 }
148 };
149 }
150 mac!()
151 }
152
153 fn parts_from_macro() -> impl Future<Output = u32> {
154 macro_rules! mac {
155 ($e: expr) => {
156 // Do not lint: `$e` might not always be side-effect free
157 async { $e.await }
158 };
159 }
160 mac!(async { 42 })
161 }
162
163 fn safe_parts_from_macro() -> impl Future<Output = u32> {
164 macro_rules! mac {
165 ($e: expr) => {
166 // Lint
167 async { $e }
168 };
169 }
170 mac!(42)
171 }
172
173 fn parts_from_macro_deep() -> impl Future<Output = u32> {
174 macro_rules! mac {
175 ($e: expr) => {
176 // Do not lint: `$e` might not always be side-effect free
177 async { ($e,).0.await }
178 };
179 }
180 let f = std::future::ready(42);
181 mac!(f)
182 }
183
184 fn await_from_macro_deep() -> impl Future<Output = u32> {
185 macro_rules! mac {
186 ($e:expr) => {{ $e }.await};
187 }
188 // Do not lint: the macro may change in the future
189 // or return different things depending on its argument
190 async { mac!(async { 42 }) }
191 }