Skip to content

E0716 proposes let binding to invalid places in the code #119686

@trrk

Description

@trrk

Code

use async_trait::async_trait; // async-trait 0.1.76;
struct A;

#[async_trait]
pub trait Trait1 {
    async fn func() -> ();
}

#[async_trait]
impl Trait1 for A {
    async fn func() -> () {
        let p = std::convert::identity(&("".to_string()));
        let _q = p;
    }
}

Current output

error[E0716]: temporary value dropped while borrowed
  --> src/lib.rs:12:41
   |
12 |         let p = std::convert::identity(&("".to_string()));
   |                                         ^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
   |                                         |
   |                                         creates a temporary value which is freed while still in use
13 |         let _q = p;
   |                  - borrow later used here
   |
help: consider using a `let` binding to create a longer lived value
   |
11 ~     async fn func() -> () let binding = ("".to_string());
12 ~     {
13 ~         let p = std::convert::identity(&binding);
   |

Desired output

...

help: consider using a `let` binding to create a longer lived value
   |
11 ~     async fn func() -> ()
12 ~     {
13 ~         let binding = ("".to_string());
14 ~         let p = std::convert::identity(&binding);
   |

Rationale and extra context

No response

Other cases

No response

Rust Version

local environment

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

I have confirmed that this issue also occurs in Rust Playground.

- Stable channel: 1.75.0
- Beta channel: 1.76.0-beta.1 (2023-12-21 0e09125c6c3c2fd70d7d)
- Nightly channel: 1.77.0-nightly (2024-01-06 b6a8c762eed0ae038365)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions