Skip to content

Suggesting adding lifetime to return type #94492

Closed
@kckeiks

Description

@kckeiks

When there is a (elided) lifetime mismatch, we output a hint to introduce a lifetime but the hint does not suggest adding a lifetime to the return type.

Given the following code:

trait T {
    fn foo(&self, x: &i32) -> Option<&i32> {
        Some(x)
    }
}

The current output is:



error[E0623]: lifetime mismatch
  --> src/main.rs:11:9
   |
10 |     fn foo(&self, x: &i32) -> Option<&i32> {
   |                      ----     ------------
   |                      |
   |                      this parameter and the return type are declared with different lifetimes...
11 |         Some(x)
   |         ^^^^^^^ ...but data from `x` is returned here
   |
   = note: each elided lifetime in input position becomes a distinct lifetime
help: consider introducing a named lifetime parameter
   |
10 |     fn foo<'a>(&'a self, x: &'a i32) -> Option<&i32> {
   |           ++++  ++           ++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0623`.


Ideally the output should also suggest adding 'a to the return type:


error[E0623]: lifetime mismatch
  --> src/main.rs:11:9
   |
10 |     fn foo(&self, x: &i32) -> Option<&i32> {
   |                      ----     ------------
   |                      |
   |                      this parameter and the return type are declared with different lifetimes...
11 |         Some(x)
   |         ^^^^^^^ ...but data from `x` is returned here
   |
   = note: each elided lifetime in input position becomes a distinct lifetime
help: consider introducing a named lifetime parameter
   |
10 |     fn foo<'a>(&'a self, x: &'a i32) -> Option<&'a i32> {
   |           ++++  ++           ++                 ++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0623`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`P-lowLow 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