Skip to content

while_let_loop suggestion does not compile #5717

Closed
@mahkoh

Description

@mahkoh
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Clone)]
struct X {
    x: Rc<RefCell<Option<X>>>,
}

fn main() {
    let mut x = X {
        x: Rc::new(RefCell::new(None)),
    };
    loop {
        let tmp = match *x.x.borrow() {
            Some(ref y) => y.clone(),
            _ => break,
        };
        x = tmp;
    }  // try: `while let Some(ref y) = *x.x.borrow() { .. }
}

But

error[E0506]: cannot assign to `x` because it is borrowed
  --> src/main.rs:22:9
   |
20 |     while let Some(ref y) = *x.x.borrow() {
   |                              ------------
   |                              |          |
   |                              |          ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::cell::Ref<'_, std::option::Option<X>>`
   |                              borrow of `x` occurs here
   |                              a temporary with access to the borrow is created here ...
21 |         let tmp = y.clone();
22 |         x = tmp;
   |         ^ assignment to borrowed `x` occurs here

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions