Skip to content

Suggest removing a borrow to fix lifetime error (replacing autoderef'ed expr with a mut-coercible expr) #150043

@A4-Tacks

Description

@A4-Tacks

Code

fn foo(r: &mut i32) -> &i32 { &r }

Current output

error[E0515]: cannot return reference to function parameter `r` --> src/bin/main.rs:4:5 | 4 | &r | ^^ returns a reference to data owned by the current function For more information about this error, try `rustc --explain E0515`. error: could not compile `quick-rs` (bin "main") due to 1 previous error

Desired output

error[E0515]: cannot return reference to function parameter `r` --> src/bin/main.rs:4:5 | 4 | &r | ^ help: remove this ref | ^^ returns a reference to data owned by the current function For more information about this error, try `rustc --explain E0515`. error: could not compile `quick-rs` (bin "main") due to 1 previous error

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.91.0-nightly (040a98af7 2025-08-20) binary: rustc commit-hash: 040a98af70f0a7da03f3d5356531b28a2a7a77e4 commit-date: 2025-08-20 host: aarch64-unknown-linux-gnu release: 1.91.0-nightly LLVM version: 21.1.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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