rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.3k stars 1.52k forks source link

await_holding_borrow - like await_holding_lock, but for RefCell #13328

Open lylythechosenone opened 4 weeks ago

lylythechosenone commented 4 weeks ago

What it does

This lint should warn users attempting to hold a RefCell's Ref or RefMut guards across an await point.

Advantage

Even in a single-threaded environment, external code can run during awaits. This code may attempt to borrow the same RefCell, causing a panic.

Drawbacks

Holding a borrow may be intentional, although it likely isn't.

Example

async {
    let my_cell: RefCell<_> = /* divine it from somewhere */;
    let my_borrow = my_cell.borrow();
    some_future().await;
    println!("The value is {}!", *my_borrow);
}

There is no trivial improvement to this code. Some examples of a fix might be

Jarcho commented 2 weeks ago

There's await_holding_invalid_type to handle that. Will require configuring the lint.

dwrensha commented 5 days ago

Is this the same as https://rust-lang.github.io/rust-clippy/master/index.html#/await_holding_refcell_ref ?