]> git.lizzy.rs Git - rust.git/blob - tests/ui/await_holding_lock.rs
Lint for holding locks across await points
[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 fn main() {
38     let m = Mutex::new(100);
39     good(&m);
40     bad(&m);
41     also_bad(&m);
42 }