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