Skip to content

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

Open
@kpozin

Description

@kpozin

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions