Skip to content

On temporary value dropped while borrowed error use structured suggestion #61405

Closed
@estebank

Description

@estebank

From https://www.reddit.com/r/rust/comments/bv90s7/temporary_value_dropped_while_borrowed/

We currently supply the following diagnostic:

let mut words = con.unwrap().split_whitespace().peekable();
                ^^^^^^^^^^^^                              - temporary value is freed at the
                |                                                     end of this statement
   |            |
   |            creates a temporary which is freed while still in use
   |
   |             while words.peek().is_some() {
   |                   ----- borrow later used here
   |
   = note: consider using a `let` binding to create a longer lived value

The note should be a structured suggestion that looks at the current line, figures out the indentation, finds the span for the beginning of the line and suggests adding a line above with a let binding. This should make it clearer what we're suggesting. Additionally, it'd be nice if we could add either a succinct explanation or a link to the book with an explanation of what might be happening here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.P-mediumMedium priorityT-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