You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Lifetime elision][elision] in function signatures is an ergonomic aspect of the Rust language, but it can also be a stumbling point for newcomers and experts alike. This is especially true when lifetimes are inferred in types where it isn't syntactically obvious that a lifetime is even present:
51
+
52
+
```rust
53
+
// The returned type `std::slice::Iter` has a lifetime,
54
+
// but there's no visual indication of that.
55
+
//
56
+
// Lifetime elision infers the lifetime of the return
57
+
// value to be the same as the argument `scores`.
58
+
fnitems(scores:&[u8]) ->std::slice::Iter<u8> {
59
+
scores.iter()
60
+
}
61
+
```
62
+
63
+
Code like this will now produce a warning by default:
64
+
65
+
```text
66
+
warning: hiding a lifetime that's elided elsewhere is confusing
We [first attempted][elided_lifetime_in_path] to improve this situation back in 2018 as part of the [`rust_2018_idioms`][2018-by-default] lint group, but [strong feedback][bevy] about the `elided_lifetimes_in_paths` lint showed that it was too blunt of a hammer as it warns about lifetimes which don't matter to understand the function:
// Knowing that `Formatter` has a lifetime does not help the programmer
93
+
"howdy".fmt(f)
94
+
}
95
+
}
96
+
```
97
+
98
+
We then realized that the confusion we want to eliminate occurs when both
99
+
100
+
1. lifetime elision inference rules *connect* an input lifetime to an output lifetime
101
+
2. it's not syntactically obvious that a lifetime exists
102
+
103
+
There are two pieces of Rust syntax that indicate that a lifetime exists: `&` and `'`, with `'` being subdivided into the inferred lifetime `'_` and named lifetimes `'a`. When a type uses a named lifetime, lifetime elision will not infer a lifetime for that type. Using these criteria, we can construct three groups:
104
+
105
+
| Self-evident it has a lifetime | Allow lifetime elision to infer a lifetime | Examples |
The `mismatched_lifetime_syntaxes` lint checks that the inputs and outputs of a function belong to the same group. For the initial motivating example above, `&[u8]` falls into the second group while `std::slice::Iter<u8>` falls into the first group. We say that the lifetimes in the first group are *hidden*.
112
+
113
+
Because the input and output lifetimes belong to different groups, the lint will warn about this function, reducing confusion about when a value has a meaningful lifetime that isn't visually obvious.
114
+
115
+
The `mismatched_lifetime_syntaxes` lint supersedes the `elided_named_lifetimes` lint, which did something similar for named lifetimes specifically.
116
+
117
+
Future work on the `elided_lifetimes_in_paths` lint intends to split it into more focused sub-lints with an eye to warning about a subset of them eventually.
0 commit comments