fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
4.1 KiB191 linesraw
1#![allow(unused, clippy::manual_async_fn)]
2#![warn(clippy::redundant_async_block)]
3
4use std::future::Future;
5
6async fn func1(n: usize) -> usize {
7    n + 1
8}
9
10async 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
17fn 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)]
40fn capture_local() -> impl Future<Output = i32> {
41    let fut = async { 17 };
42    // Lint
43    fut
44}
45
46fn 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)]
53fn capture_arg(s: &str) -> impl Future<Output = &str> {
54    let fut = async move { s };
55    // Lint
56    fut
57}
58
59fn capture_future_arg<T>(f: impl Future<Output = T>) -> impl Future<Output = T> {
60    // Lint
61    f
62}
63
64fn capture_func_result<FN, F, T>(f: FN) -> impl Future<Output = T>
65where
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
73fn 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
78fn await_in_async<F, R>(f: F) -> impl Future<Output = u32>
79where
80    F: FnOnce() -> R,
81    R: Future<Output = u32>,
82{
83    // Lint
84    async { f().await + 1 }
85}
86
87#[derive(Debug, Clone)]
88struct F {}
89
90impl F {
91    async fn run(&self) {}
92}
93
94pub 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
102fn spawn<F: Future + 'static>(_: F) {}
103
104async fn work(_: &str) {}
105
106fn 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
112fn 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
123fn 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
133fn 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
143fn all_from_macro() -> impl Future<Output = u32> {
144    macro_rules! mac {
145        () => {
146            // Lint
147            async { 42 }
148        };
149    }
150    mac!()
151}
152
153fn 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
163fn 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
173fn 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
184fn 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}