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

while_let_loop suggestion does not compile #5717

Closed
mahkoh opened this issue Jun 14, 2020 · 1 comment · Fixed by #8666
Closed

while_let_loop suggestion does not compile #5717

mahkoh opened this issue Jun 14, 2020 · 1 comment · Fixed by #8666
Labels
C-bug Category: Clippy is not doing the correct thing E-hard Call for participation: This a hard problem and requires more experience or effort to work on I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@mahkoh
Copy link

mahkoh commented Jun 14, 2020

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
@flip1995 flip1995 added E-hard Call for participation: This a hard problem and requires more experience or effort to work on C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jun 15, 2020
@camsteffen
Copy link
Contributor

camsteffen commented Jul 6, 2020

I've been trying to understand this for fun and I came up with a (kinda) more minimal example.

struct X;

impl X {
    fn y(&self) -> Y<'_> {
        Y(self)
    }
}

struct Y<'a>(&'a X);

impl Drop for Y<'_> {
    fn drop(&mut self) {}
}

impl<'a> Y<'a> {
    fn z(&self) -> Option<X> {
        None
    }
}

fn main() {
    let mut x = X;
    loop {
        let tmp = match x.y().z() {
            Some(xx) => xx,
            _ => break,
        };
        x = tmp;
    }
}

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 E-hard Call for participation: This a hard problem and requires more experience or effort to work on I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants