Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clippy::await_holding_refcell_ref appears to assume out-of-scope variables are being held across await points #10124

Open
kpozin opened this issue Dec 28, 2022 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@kpozin
Copy link

kpozin commented Dec 28, 2022

The following code

use anyhow::Error;
use std::cell::RefCell;

pub struct Outer {
    inner: RefCell<Inner>,
}

impl Outer {
    pub fn new() -> Self {
        Self {
            inner: RefCell::new(Inner {
                val: 0,
                flag: false,
            }),
        }
    }
}

pub struct Inner {
    val: u32,
    flag: bool,
}

impl Inner {
    
    fn val(&self) -> u32 {
        self.val
    }
}

pub async fn do_something() -> Result<(), Error> {
    let outer = Outer::new();
    if let Some(val) = {
        let inner = outer.inner.borrow();
        inner.flag.then(move || inner.val())
    } {
        do_something_else(val).await?;
    }
    Ok(())
}

async fn do_something_else(val: u32) -> Result<(), Error> {
    println!("{val}");
    Ok(())
}

yields the following unexpected clippy warning:

warning: this `RefCell` reference is held across an `await` point
  --> src/lib.rs:37:9
   |
37 |         inner.flag.then(move || inner.val())
   |         ^^^^^
   |
   = help: ensure the reference is dropped before calling `await`
note: these are all the `await` points this reference is held through
  --> src/lib.rs:35:5
   |
35 | /     if let Some(val) = {
36 | |         let inner = outer.inner.borrow();
37 | |         inner.flag.then(move || inner.val())
38 | |     } {
39 | |         do_something_else(val).await?;
40 | |     }
   | |_____^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
   = note: `#[warn(clippy::await_holding_refcell_ref)]` on by default

This probably falls under rust-lang/rust#69663.

Playground link

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=75f2f97ff4f11d2d957b25df47d61c26

Meta

Nightly channel
Build using the Nightly version: 1.68.0-nightly

(2022-12-27 92c1937a90e5b6f20fa6)
@kpozin kpozin added the C-bug Category: Clippy is not doing the correct thing label Dec 28, 2022
@matthiaskrgr matthiaskrgr transferred this issue from rust-lang/rust Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

1 participant