]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/src/docs/await_holding_refcell_ref.txt
New upstream version 1.66.0+dfsg1
[rustc.git] / src / tools / clippy / src / docs / await_holding_refcell_ref.txt
CommitLineData
f2b60f7d
FG
1### What it does
2Checks 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
6at runtime. Holding onto a `RefCell` ref across an `await` suspension point
7risks panics from a mutable ref shared while other refs are outstanding.
8
9### Known problems
10Will report false positive for explicitly dropped refs
11([#6353](https://github.com/rust-lang/rust-clippy/issues/6353)). A workaround for this is
12to wrap the `.borrow[_mut]()` call in a block instead of explicitly dropping the ref.
13
14### Example
15```
16async fn foo(x: &RefCell<u32>) {
17 let mut y = x.borrow_mut();
18 *y += 1;
19 baz().await;
20}
21
22async 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
30Use instead:
31```
32async fn foo(x: &RefCell<u32>) {
33 {
34 let mut y = x.borrow_mut();
35 *y += 1;
36 }
37 baz().await;
38}
39
40async 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```