summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/await_holding_lock.rs
blob: 57e5b55045b95de03495fa9a9ca22db68a6657b1 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#![warn(clippy::await_holding_lock)]

// When adding or modifying a test, please do the same for parking_lot::Mutex.
mod std_mutex {
    use super::baz;
    use std::sync::{Mutex, RwLock};

    pub async fn bad(x: &Mutex<u32>) -> u32 {
        let guard = x.lock().unwrap();
        baz().await
    }

    pub async fn good(x: &Mutex<u32>) -> u32 {
        {
            let guard = x.lock().unwrap();
            let y = *guard + 1;
        }
        baz().await;
        let guard = x.lock().unwrap();
        47
    }

    pub async fn bad_rw(x: &RwLock<u32>) -> u32 {
        let guard = x.read().unwrap();
        baz().await
    }

    pub async fn bad_rw_write(x: &RwLock<u32>) -> u32 {
        let mut guard = x.write().unwrap();
        baz().await
    }

    pub async fn good_rw(x: &RwLock<u32>) -> u32 {
        {
            let guard = x.read().unwrap();
            let y = *guard + 1;
        }
        {
            let mut guard = x.write().unwrap();
            *guard += 1;
        }
        baz().await;
        let guard = x.read().unwrap();
        47
    }

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

        let guard = x.lock().unwrap();

        let second = baz().await;

        let third = baz().await;

        first + second + third
    }

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

        let second = {
            let guard = x.lock().unwrap();
            baz().await
        };

        let third = baz().await;

        first + second + third
    }

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

// When adding or modifying a test, please do the same for std::Mutex.
mod parking_lot_mutex {
    use super::baz;
    use parking_lot::{Mutex, RwLock};

    pub async fn bad(x: &Mutex<u32>) -> u32 {
        let guard = x.lock();
        baz().await
    }

    pub async fn good(x: &Mutex<u32>) -> u32 {
        {
            let guard = x.lock();
            let y = *guard + 1;
        }
        baz().await;
        let guard = x.lock();
        47
    }

    pub async fn bad_rw(x: &RwLock<u32>) -> u32 {
        let guard = x.read();
        baz().await
    }

    pub async fn bad_rw_write(x: &RwLock<u32>) -> u32 {
        let mut guard = x.write();
        baz().await
    }

    pub async fn good_rw(x: &RwLock<u32>) -> u32 {
        {
            let guard = x.read();
            let y = *guard + 1;
        }
        {
            let mut guard = x.write();
            *guard += 1;
        }
        baz().await;
        let guard = x.read();
        47
    }

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

        let guard = x.lock();

        let second = baz().await;

        let third = baz().await;

        first + second + third
    }

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

        let second = {
            let guard = x.lock();
            baz().await
        };

        let third = baz().await;

        first + second + third
    }

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

async fn baz() -> u32 {
    42
}

async fn no_await(x: std::sync::Mutex<u32>) {
    let mut guard = x.lock().unwrap();
    *guard += 1;
}

// FIXME: FP, because the `MutexGuard` is dropped before crossing the await point. This is
// something the needs to be fixed in rustc. There's already drop-tracking, but this is currently
// disabled, see rust-lang/rust#93751. This case isn't picked up by drop-tracking though. If the
// `*guard += 1` is removed it is picked up.
async fn dropped_before_await(x: std::sync::Mutex<u32>) {
    let mut guard = x.lock().unwrap();
    *guard += 1;
    drop(guard);
    baz().await;
}

fn main() {
    let m = std::sync::Mutex::new(100);
    std_mutex::good(&m);
    std_mutex::bad(&m);
    std_mutex::also_bad(&m);
    std_mutex::not_good(&m);
    std_mutex::block_bad(&m);

    let m = parking_lot::Mutex::new(100);
    parking_lot_mutex::good(&m);
    parking_lot_mutex::bad(&m);
    parking_lot_mutex::also_bad(&m);
    parking_lot_mutex::not_good(&m);
}