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::let_and_return suggestion causes error because value does not live long enough #13735

Open
antonilol opened this issue Nov 26, 2024 · 1 comment
Labels
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

Comments

@antonilol
Copy link
Contributor

Summary

title, but that is only an assumption about what happens. It seems to me that this should be able to compile, because the closure returns something that lives for 'static.

This is a stripped down bug reproducer based on real code, this explains the nested RefCell (from a graph) and &usize (replaced a non-copy struct with usize).

The only difference between the function bodies of reproduce_bug and no_bug is that reproduce_bug has the output of some_function(...) wrapped with Some(...?).
In both functions the code fails to compile when the value of x is returned directly, but clippy only suggests this for reproduce_bug

Reproducer

I tried this code:

use core::cell::RefCell;

fn some_function(_: &usize) -> Option<bool> {
    Some(true)
}

pub fn reproduce_bug() -> Option<bool> {
    let nested_ref_cell = RefCell::new(RefCell::new(123usize));

    some_function(&456).and_then(|_| {
        let ref_cell_ref = nested_ref_cell.borrow();
        let inner_ref_cell = &*ref_cell_ref;

        let x = Some(some_function(&inner_ref_cell.borrow())?);
        x
    })
}

pub fn no_bug() -> Option<bool> {
    let nested_ref_cell = RefCell::new(RefCell::new(123usize));

    some_function(&456).and_then(|_| {
        let ref_cell_ref = nested_ref_cell.borrow();
        let inner_ref_cell = &*ref_cell_ref;

        let x = some_function(&inner_ref_cell.borrow());
        x
    })
}

I expected to see this happen:

Instead, this happened:

Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Additional Labels

@rustbot label +I-suggestion-causes-error

@antonilol antonilol added the C-bug Category: Clippy is not doing the correct thing label Nov 26, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Nov 26, 2024
@y21
Copy link
Member

y21 commented Nov 26, 2024

The suggestion compiles with edition 2024 where the lifetime of temporaries in tail expression was changed to allow this, so probably the same issue as #12831, #11308 and #9794.

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants