]> git.lizzy.rs Git - rust.git/blob - tests/ui/await_holding_lock.rs
iterate List by value
[rust.git] / 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 fn block_bad(x: &Mutex<u32>) -> impl std::future::Future<Output = u32> + '_ {
51     async move {
52         let guard = x.lock().unwrap();
53         baz().await
54     }
55 }
56
57 fn main() {
58     let m = Mutex::new(100);
59     good(&m);
60     bad(&m);
61     also_bad(&m);
62     not_good(&m);
63     block_bad(&m);
64 }