rust-lang / rust-lang/rust

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

Open
#150,043 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-diagnostics D-lack-of-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/bin/main.rs and run it against the reported Rust version to confirm the E0515 diagnostic. Trace the compiler's E0515 diagnostic handling and add coverage for this case, with done defined as emitting the suggested “remove this ref” help text shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.