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

fix suggestion for E0716 (temporary value dropped while borrowed) #137486

Open
reddevilmidzy opened this issue Feb 23, 2025 · 0 comments
Open

fix suggestion for E0716 (temporary value dropped while borrowed) #137486

reddevilmidzy opened this issue Feb 23, 2025 · 0 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@reddevilmidzy
Copy link
Contributor

I tried this code:

fn main() {
    let mut s = String::from("hello");

    let mut ref_s = &mut s;

    ref_s = &mut String::from("world");

    print!("r1 = {}", ref_s);
}

The Rust compiler suggests an incorrect fix:

error[E0716]: temporary value dropped while borrowed
 --> src/main.rs:6:18
  |
6 |     ref_s = &mut String::from("world");
  |                  ^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
  |                  |
  |                  creates a temporary value which is freed while still in use
7 |
8 |     print!("r1 = {}", ref_s);
  |                       ----- borrow later used here
  |
help: consider using a `let` binding to create a longer lived value
  |
6 ~     let binding = String::from("world");
7 ~     ref_s = &mut binding;
  |

However, this suggestion leads to another compilation error because binding is immutable:

error[E0596]: cannot borrow `binding` as mutable, as it is not declared as mutable
 --> src/main.rs:7:13
  |
7 |     ref_s = &mut binding;
  |             ^^^^^^^^^^^^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
6 |     let mut binding = String::from("world");
  |         +++

This forces the user to encounter another error message before arriving at the correct fix.

Expected Fix

The Rust compiler should directly suggest the correct fix:

help: consider using a `let` binding to create a longer lived value
   |
6  ~    let mut binding = String::from("world");
7  ~    ref_s = &mut binding;
   |

This avoids an unnecessary additional compilation error, improving the developer experience.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-pc-windows-gnu
release: 1.83.0
LLVM version: 19.1.1
@reddevilmidzy reddevilmidzy added the C-bug Category: This is a bug. label Feb 23, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 23, 2025
@jieyouxu jieyouxu added A-diagnostics Area: Messages for errors, warnings, and lints A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 23, 2025
@chenyukang chenyukang self-assigned this Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
Development

No branches or pull requests

4 participants