Always constrain the return type in lifetime suggestion

```
error: lifetime may not live long enough
 --> f205.rs:8:16
  |
7 |     fn resolve_symbolic_reference(&self, reference: Option<Reference>) -> Option<Reference> {
  |                                   -      --------- has type `Option<Reference<'1>>`
  |                                   |
  |                                   let's call the lifetime of this reference `'2`
8 |         return reference;
  |                ^^^^^^^^^ method was supposed to return data with lifetime `'2` but it is returning data with lifetime `'1`
  |
help: consider introducing a named lifetime parameter
  |
7 |     fn resolve_symbolic_reference<'a>(&'a self, reference: Option<Reference<'a>>) -> Option<Reference<'a>> {
  |                                  ++++  ++                                  ++++                      ++++
```

The correct suggestion would be

```
help: consider introducing a named lifetime parameter
  |
7 |     fn resolve_symbolic_reference<'a>(&self, reference: Option<Reference<'a>>) -> Option<Reference<'a>> {
  |                                  ++++                                   ++++                      ++++
```

but we are not doing the analysis to detect that yet. If we constrain `&'a self`, then the return type with a borrow will implicitly take its lifetime from `'a`, it is better to make it explicit in the suggestion, in case that `&self` *doesn't* need to be `'a`, but the return does.
This commit is contained in:
Esteban Küber 2024-05-05 03:42:47 +00:00
parent 9f730e92f2
commit 120049fab4
11 changed files with 77 additions and 72 deletions

View file

@ -450,6 +450,11 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
}; };
visitor.visit_ty(self.ty_sub); visitor.visit_ty(self.ty_sub);
visitor.visit_ty(self.ty_sup); visitor.visit_ty(self.ty_sup);
if let Some(fn_decl) = node.fn_decl()
&& let hir::FnRetTy::Return(ty) = fn_decl.output
{
visitor.visit_ty(ty);
}
if visitor.suggestions.is_empty() { if visitor.suggestions.is_empty() {
return false; return false;
} }

View file

@ -29,8 +29,8 @@ LL | Some(entry) => Ok(entry),
| |
help: consider introducing a named lifetime parameter help: consider introducing a named lifetime parameter
| |
LL | fn attemptTraverse<'a>(&'a self, room: &'a Room, directionStr: &str) -> Result<&Room, &str> { LL | fn attemptTraverse<'a>(&'a self, room: &'a Room, directionStr: &str) -> Result<&'a Room, &'a str> {
| ++++ ++ ++ | ++++ ++ ++ ++ ++
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -11,8 +11,8 @@ LL | x
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn foo<'a>(&'a self, x: &'a i32) -> &i32 { LL | fn foo<'a>(&'a self, x: &'a i32) -> &'a i32 {
| ++ | ++ ++
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -10,8 +10,8 @@ LL | x
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn foo<'a>(&'a self, x: &'a i32) -> &i32 { LL | fn foo<'a>(&'a self, x: &'a i32) -> &'a i32 {
| ++ ++ | ++ ++ ++
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -10,8 +10,8 @@ LL | if true { x } else { self }
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn foo<'a>(&'a self, x: &'a Foo) -> &Foo { LL | fn foo<'a>(&'a self, x: &'a Foo) -> &'a Foo {
| ++ ++ | ++ ++ ++
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -9,8 +9,8 @@ LL | fn a(self: Pin<&Foo>, f: &Foo) -> &Foo { f }
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn a<'a>(self: Pin<&'a Foo>, f: &'a Foo) -> &Foo { f } LL | fn a<'a>(self: Pin<&'a Foo>, f: &'a Foo) -> &'a Foo { f }
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:9:69 --> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:9:69
@ -23,8 +23,8 @@ LL | fn c(self: Pin<&Self>, f: &Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self,
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn c<'a>(self: Pin<&'a Self>, f: &'a Foo, g: &Foo) -> (Pin<&Foo>, &Foo) { (self, f) } LL | fn c<'a>(self: Pin<&'a Self>, f: &'a Foo, g: &Foo) -> (Pin<&'a Foo>, &'a Foo) { (self, f) }
| ++++ ++ ++ | ++++ ++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:15:58 --> $DIR/arbitrary_self_types_pin_lifetime_mismatch.rs:15:58
@ -36,8 +36,8 @@ LL | fn bar<'a>(self: Alias<&Self>, arg: &'a ()) -> &() { arg }
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn bar<'a>(self: Alias<&'a Self>, arg: &'a ()) -> &() { arg } LL | fn bar<'a>(self: Alias<&'a Self>, arg: &'a ()) -> &'a () { arg }
| ++ | ++ ++
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View file

@ -10,8 +10,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &u32 { LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/lt-ref-self.rs:18:9 --> $DIR/lt-ref-self.rs:18:9
@ -25,8 +25,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &u32 { LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/lt-ref-self.rs:23:9 --> $DIR/lt-ref-self.rs:23:9
@ -40,8 +40,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &u32 { LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/lt-ref-self.rs:28:9 --> $DIR/lt-ref-self.rs:28:9
@ -55,8 +55,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &u32 { LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/lt-ref-self.rs:33:9 --> $DIR/lt-ref-self.rs:33:9
@ -70,8 +70,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &u32 { LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/lt-ref-self.rs:38:9 --> $DIR/lt-ref-self.rs:38:9
@ -85,8 +85,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_pin_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &u32 { LL | fn box_pin_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: aborting due to 6 previous errors error: aborting due to 6 previous errors

View file

@ -10,8 +10,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_self<'a>(&'a mut self, f: &'a u32) -> &u32 { LL | fn ref_self<'a>(&'a mut self, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-self.rs:18:9 --> $DIR/ref-mut-self.rs:18:9
@ -25,8 +25,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_Self<'a>(self: &'a mut Self, f: &'a u32) -> &u32 { LL | fn ref_Self<'a>(self: &'a mut Self, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-self.rs:23:9 --> $DIR/ref-mut-self.rs:23:9
@ -40,8 +40,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_ref_Self<'a>(self: Box<&'a mut Self>, f: &'a u32) -> &u32 { LL | fn box_ref_Self<'a>(self: Box<&'a mut Self>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-self.rs:28:9 --> $DIR/ref-mut-self.rs:28:9
@ -55,8 +55,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn pin_ref_Self<'a>(self: Pin<&'a mut Self>, f: &'a u32) -> &u32 { LL | fn pin_ref_Self<'a>(self: Pin<&'a mut Self>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-self.rs:33:9 --> $DIR/ref-mut-self.rs:33:9
@ -70,8 +70,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a mut Self>>, f: &'a u32) -> &u32 { LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a mut Self>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-self.rs:38:9 --> $DIR/ref-mut-self.rs:38:9
@ -85,8 +85,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a mut Self>>, f: &'a u32) -> &u32 { LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a mut Self>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: aborting due to 6 previous errors error: aborting due to 6 previous errors

View file

@ -10,8 +10,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_Struct<'a>(self: &'a mut Struct, f: &'a u32) -> &u32 { LL | fn ref_Struct<'a>(self: &'a mut Struct, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-struct.rs:16:9 --> $DIR/ref-mut-struct.rs:16:9
@ -25,8 +25,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_ref_Struct<'a>(self: Box<&'a mut Struct>, f: &'a u32) -> &u32 { LL | fn box_ref_Struct<'a>(self: Box<&'a mut Struct>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-struct.rs:21:9 --> $DIR/ref-mut-struct.rs:21:9
@ -40,8 +40,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn pin_ref_Struct<'a>(self: Pin<&'a mut Struct>, f: &'a u32) -> &u32 { LL | fn pin_ref_Struct<'a>(self: Pin<&'a mut Struct>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-struct.rs:26:9 --> $DIR/ref-mut-struct.rs:26:9
@ -55,8 +55,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_box_ref_Struct<'a>(self: Box<Box<&'a mut Struct>>, f: &'a u32) -> &u32 { LL | fn box_box_ref_Struct<'a>(self: Box<Box<&'a mut Struct>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-mut-struct.rs:31:9 --> $DIR/ref-mut-struct.rs:31:9
@ -70,8 +70,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_pin_ref_Struct<'a>(self: Box<Pin<&'a mut Struct>>, f: &'a u32) -> &u32 { LL | fn box_pin_ref_Struct<'a>(self: Box<Pin<&'a mut Struct>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: aborting due to 5 previous errors error: aborting due to 5 previous errors

View file

@ -10,8 +10,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &u32 { LL | fn ref_self<'a>(&'a self, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-self.rs:28:9 --> $DIR/ref-self.rs:28:9
@ -25,8 +25,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &u32 { LL | fn ref_Self<'a>(self: &'a Self, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-self.rs:33:9 --> $DIR/ref-self.rs:33:9
@ -40,8 +40,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &u32 { LL | fn box_ref_Self<'a>(self: Box<&'a Self>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-self.rs:38:9 --> $DIR/ref-self.rs:38:9
@ -55,8 +55,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &u32 { LL | fn pin_ref_Self<'a>(self: Pin<&'a Self>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-self.rs:43:9 --> $DIR/ref-self.rs:43:9
@ -70,8 +70,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &u32 { LL | fn box_box_ref_Self<'a>(self: Box<Box<&'a Self>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-self.rs:48:9 --> $DIR/ref-self.rs:48:9
@ -85,8 +85,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &u32 { LL | fn box_pin_ref_Self<'a>(self: Box<Pin<&'a Self>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-self.rs:53:9 --> $DIR/ref-self.rs:53:9
@ -100,8 +100,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn wrap_ref_Self_Self<'a>(self: Wrap<&'a Self, Self>, f: &'a u8) -> &u8 { LL | fn wrap_ref_Self_Self<'a>(self: Wrap<&'a Self, Self>, f: &'a u8) -> &'a u8 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: aborting due to 7 previous errors error: aborting due to 7 previous errors

View file

@ -10,8 +10,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn ref_Struct<'a>(self: &'a Struct, f: &'a u32) -> &u32 { LL | fn ref_Struct<'a>(self: &'a Struct, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-struct.rs:16:9 --> $DIR/ref-struct.rs:16:9
@ -25,8 +25,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_ref_Struct<'a>(self: Box<&'a Struct>, f: &'a u32) -> &u32 { LL | fn box_ref_Struct<'a>(self: Box<&'a Struct>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-struct.rs:21:9 --> $DIR/ref-struct.rs:21:9
@ -40,8 +40,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn pin_ref_Struct<'a>(self: Pin<&'a Struct>, f: &'a u32) -> &u32 { LL | fn pin_ref_Struct<'a>(self: Pin<&'a Struct>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-struct.rs:26:9 --> $DIR/ref-struct.rs:26:9
@ -55,8 +55,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_box_ref_Struct<'a>(self: Box<Box<&'a Struct>>, f: &'a u32) -> &u32 { LL | fn box_box_ref_Struct<'a>(self: Box<Box<&'a Struct>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: lifetime may not live long enough error: lifetime may not live long enough
--> $DIR/ref-struct.rs:31:9 --> $DIR/ref-struct.rs:31:9
@ -70,8 +70,8 @@ LL | f
| |
help: consider introducing a named lifetime parameter and update trait if needed help: consider introducing a named lifetime parameter and update trait if needed
| |
LL | fn box_pin_Struct<'a>(self: Box<Pin<&'a Struct>>, f: &'a u32) -> &u32 { LL | fn box_pin_Struct<'a>(self: Box<Pin<&'a Struct>>, f: &'a u32) -> &'a u32 {
| ++++ ++ ++ | ++++ ++ ++ ++
error: aborting due to 5 previous errors error: aborting due to 5 previous errors