]> git.lizzy.rs Git - rust.git/blob - tests/ui/await_holding_lock.rs
Auto merge of #8374 - Alexendoo:bless-revisions, r=camsteffen
[rust.git] / tests / ui / await_holding_lock.rs
1 #![warn(clippy::await_holding_lock)]
2
3 use std::sync::Mutex;
4
5 async fn bad(x: &Mutex<u32>) -> u32 {
6     let guard = x.lock().unwrap();
7     baz().await
8 }
9
10 async fn good(x: &Mutex<u32>) -> u32 {
11     {
12         let guard = x.lock().unwrap();
13         let y = *guard + 1;
14     }
15     baz().await;
16     let guard = x.lock().unwrap();
17     47
18 }
19
20 async fn baz() -> u32 {
21     42
22 }
23
24 async fn also_bad(x: &Mutex<u32>) -> u32 {
25     let first = baz().await;
26
27     let guard = x.lock().unwrap();
28
29     let second = baz().await;
30
31     let third = baz().await;
32
33     first + second + third
34 }
35
36 async fn not_good(x: &Mutex<u32>) -> u32 {
37     let first = baz().await;
38
39     let second = {
40         let guard = x.lock().unwrap();
41         baz().await
42     };
43
44     let third = baz().await;
45
46     first + second + third
47 }
48
49 #[allow(clippy::manual_async_fn)]
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 }