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