summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/await_holding_refcell_ref.rs
blob: 23b7095de3a39ae31a36bbc37a7b625c38bec5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![warn(clippy::await_holding_refcell_ref)]

use std::cell::RefCell;

async fn bad(x: &RefCell<u32>) -> u32 {
    let b = x.borrow();
    baz().await
}

async fn bad_mut(x: &RefCell<u32>) -> u32 {
    let b = x.borrow_mut();
    baz().await
}

async fn good(x: &RefCell<u32>) -> u32 {
    {
        let b = x.borrow_mut();
        let y = *b + 1;
    }
    baz().await;
    let b = x.borrow_mut();
    47
}

async fn baz() -> u32 {
    42
}

async fn also_bad(x: &RefCell<u32>) -> u32 {
    let first = baz().await;

    let b = x.borrow_mut();

    let second = baz().await;

    let third = baz().await;

    first + second + third
}

async fn less_bad(x: &RefCell<u32>) -> u32 {
    let first = baz().await;

    let b = x.borrow_mut();

    let second = baz().await;

    drop(b);

    let third = baz().await;

    first + second + third
}

async fn not_good(x: &RefCell<u32>) -> u32 {
    let first = baz().await;

    let second = {
        let b = x.borrow_mut();
        baz().await
    };

    let third = baz().await;

    first + second + third
}

#[allow(clippy::manual_async_fn)]
fn block_bad(x: &RefCell<u32>) -> impl std::future::Future<Output = u32> + '_ {
    async move {
        let b = x.borrow_mut();
        baz().await
    }
}

fn main() {
    let rc = RefCell::new(100);
    good(&rc);
    bad(&rc);
    bad_mut(&rc);
    also_bad(&rc);
    less_bad(&rc);
    not_good(&rc);
    block_bad(&rc);
}