]> git.lizzy.rs Git - rust.git/blob - tests/ui/await_holding_refcell_ref.rs
6e330f47dfc23de5ca91cdf7cc6808db031ba8ea
[rust.git] / tests / ui / await_holding_refcell_ref.rs
1 // edition:2018
2 #![warn(clippy::await_holding_refcell_ref)]
3
4 use std::cell::RefCell;
5
6 async fn bad(x: &RefCell<u32>) -> u32 {
7     let b = x.borrow();
8     baz().await
9 }
10
11 async fn bad_mut(x: &RefCell<u32>) -> u32 {
12     let b = x.borrow_mut();
13     baz().await
14 }
15
16 async fn good(x: &RefCell<u32>) -> u32 {
17     {
18         let b = x.borrow_mut();
19         let y = *b + 1;
20     }
21     baz().await;
22     let b = x.borrow_mut();
23     47
24 }
25
26 async fn baz() -> u32 {
27     42
28 }
29
30 async fn also_bad(x: &RefCell<u32>) -> u32 {
31     let first = baz().await;
32
33     let b = x.borrow_mut();
34
35     let second = baz().await;
36
37     let third = baz().await;
38
39     first + second + third
40 }
41
42 async fn not_good(x: &RefCell<u32>) -> u32 {
43     let first = baz().await;
44
45     let second = {
46         let b = x.borrow_mut();
47         baz().await
48     };
49
50     let third = baz().await;
51
52     first + second + third
53 }
54
55 #[allow(clippy::manual_async_fn)]
56 fn block_bad(x: &RefCell<u32>) -> impl std::future::Future<Output = u32> + '_ {
57     async move {
58         let b = x.borrow_mut();
59         baz().await
60     }
61 }
62
63 fn main() {
64     let m = RefCell::new(100);
65     good(&m);
66     bad(&m);
67     bad_mut(&m);    
68     also_bad(&m);
69     not_good(&m);
70     block_bad(&m);
71 }