]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/await_holding_refcell_ref.rs
Rollup merge of #78730 - kornelski:not-inverse, r=Dylan-DPC
[rust.git] / src / tools / clippy / 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 less_bad(x: &RefCell<u32>) -> u32 {
43     let first = baz().await;
44
45     let b = x.borrow_mut();
46
47     let second = baz().await;
48
49     drop(b);
50
51     let third = baz().await;
52
53     first + second + third
54 }
55
56 async fn not_good(x: &RefCell<u32>) -> u32 {
57     let first = baz().await;
58
59     let second = {
60         let b = x.borrow_mut();
61         baz().await
62     };
63
64     let third = baz().await;
65
66     first + second + third
67 }
68
69 #[allow(clippy::manual_async_fn)]
70 fn block_bad(x: &RefCell<u32>) -> impl std::future::Future<Output = u32> + '_ {
71     async move {
72         let b = x.borrow_mut();
73         baz().await
74     }
75 }
76
77 fn main() {
78     let rc = RefCell::new(100);
79     good(&rc);
80     bad(&rc);
81     bad_mut(&rc);
82     also_bad(&rc);
83     less_bad(&rc);
84     not_good(&rc);
85     block_bad(&rc);
86 }