]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/await_holding_lock.rs
Auto merge of #84620 - Dylan-DPC:rollup-wkv97im, r=Dylan-DPC
[rust.git] / src / tools / clippy / tests / ui / await_holding_lock.rs
1 // edition:2018
2 #![warn(clippy::await_holding_lock)]
3
4 use std::sync::Mutex;
5
6 async fn bad(x: &Mutex<u32>) -> u32 {
7     let guard = x.lock().unwrap();
8     baz().await
9 }
10
11 async fn good(x: &Mutex<u32>) -> u32 {
12     {
13         let guard = x.lock().unwrap();
14         let y = *guard + 1;
15     }
16     baz().await;
17     let guard = x.lock().unwrap();
18     47
19 }
20
21 async fn baz() -> u32 {
22     42
23 }
24
25 async fn also_bad(x: &Mutex<u32>) -> u32 {
26     let first = baz().await;
27
28     let guard = x.lock().unwrap();
29
30     let second = baz().await;
31
32     let third = baz().await;
33
34     first + second + third
35 }
36
37 async fn not_good(x: &Mutex<u32>) -> u32 {
38     let first = baz().await;
39
40     let second = {
41         let guard = x.lock().unwrap();
42         baz().await
43     };
44
45     let third = baz().await;
46
47     first + second + third
48 }
49
50 #[allow(clippy::manual_async_fn)]
51 fn block_bad(x: &Mutex<u32>) -> impl std::future::Future<Output = u32> + '_ {
52     async move {
53         let guard = x.lock().unwrap();
54         baz().await
55     }
56 }
57
58 fn main() {
59     let m = Mutex::new(100);
60     good(&m);
61     bad(&m);
62     also_bad(&m);
63     not_good(&m);
64     block_bad(&m);
65 }