Skip to content

"temporary value dropped while borrowed" occurs with let-chains #100276

Closed
@aznhe21

Description

@aznhe21

I tried this code:

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

fn let_chains(entry: std::io::Result<std::fs::DirEntry>) {
    if let Ok(entry) = entry
        && let Some(s) = entry.file_name().to_str()
        && s.contains("")
    {
        println!("will fail to compile");
    }
}

fn multiple_ifs(entry: std::io::Result<std::fs::DirEntry>) {
    if let Ok(entry) = entry {
        if let Some(s) = entry.file_name().to_str() {
            if s.contains("") {
                println!("will compile successfully");
            }
        }
    }
}

I expected to see this happen: both compiles successfully

Instead, this happened: let_chains fails to compile

error[E0716]: temporary value dropped while borrowed
 --> src/lib.rs:3:26
  |
3 |         && let Some(s) = entry.file_name().to_str()
  |                          ^^^^^^^^^^^^^^^^^        - temporary value is freed at the end of this statement
  |                          |
  |                          creates a temporary which is freed while still in use
4 |         && s.contains("")
  |            -------------- borrow later used here
  |
  = note: consider using a `let` binding to create a longer lived value

For more information about this error, try `rustc --explain E0716`.
error: could not compile `rust2` due to previous error

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (d394408fb 2022-08-07)
binary: rustc
commit-hash: d394408fb38c4de61f765a3ed5189d2731a1da91
commit-date: 2022-08-07
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.F-let_chains`#![feature(let_chains)]`

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions