]> git.lizzy.rs Git - rust.git/blob - src/docs/await_holding_refcell_ref.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / await_holding_refcell_ref.txt
1 ### What it does
2 Checks for calls to await while holding a `RefCell` `Ref` or `RefMut`.
3
4 ### Why is this bad?
5 `RefCell` refs only check for exclusive mutable access
6 at runtime. Holding onto a `RefCell` ref across an `await` suspension point
7 risks panics from a mutable ref shared while other refs are outstanding.
8
9 ### Known problems
10 Will report false positive for explicitly dropped refs
11 ([#6353](https://github.com/rust-lang/rust-clippy/issues/6353)). A workaround for this is
12 to wrap the `.borrow[_mut]()` call in a block instead of explicitly dropping the ref.
13
14 ### Example
15 ```
16 async fn foo(x: &RefCell<u32>) {
17   let mut y = x.borrow_mut();
18   *y += 1;
19   baz().await;
20 }
21
22 async fn bar(x: &RefCell<u32>) {
23   let mut y = x.borrow_mut();
24   *y += 1;
25   drop(y); // explicit drop
26   baz().await;
27 }
28 ```
29
30 Use instead:
31 ```
32 async fn foo(x: &RefCell<u32>) {
33   {
34      let mut y = x.borrow_mut();
35      *y += 1;
36   }
37   baz().await;
38 }
39
40 async fn bar(x: &RefCell<u32>) {
41   {
42     let mut y = x.borrow_mut();
43     *y += 1;
44   } // y dropped here at end of scope
45   baz().await;
46 }
47 ```