1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#![warn(clippy::needless_lifetimes, clippy::elidable_lifetime_names)]
type Ref<'r> = &'r u8;
// No error; same lifetime on two params.
fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
//~v ERROR: could be elided: 'a, 'b
fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {}
// No error; bounded lifetime.
fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {}
// No error; bounded lifetime.
fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
where
'b: 'a,
{
}
struct Lt<'a, I: 'static> {
x: &'a I,
}
// No error; fn bound references `'a`.
fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
where
F: Fn(Lt<'a, I>) -> Lt<'a, I>,
{
unreachable!()
}
//~v ERROR: could be elided: 'a
fn fn_bound_2<F, I>(_m: Lt<'_, I>, _f: F) -> Lt<'_, I>
where
for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
{
unreachable!()
}
struct Foo<'a>(&'a u8);
//~v ERROR: could be elided: 'a
fn struct_with_lt(_foo: Foo<'_>) -> &str {
unimplemented!()
}
// No warning; two input lifetimes (named on the reference, anonymous on `Foo`).
fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
unimplemented!()
}
// No warning; two input lifetimes (anonymous on the reference, named on `Foo`).
fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
unimplemented!()
}
//~v ERROR: could be elided: 'b
fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str {
unimplemented!()
}
type FooAlias<'a> = Foo<'a>;
//~v ERROR: could be elided: 'a
fn alias_with_lt(_foo: FooAlias<'_>) -> &str {
unimplemented!()
}
// No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`).
fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
unimplemented!()
}
// No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`).
fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
unimplemented!()
}
//~v ERROR: could be elided: 'b
fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str {
unimplemented!()
}
// Issue #3284: give hint regarding lifetime in return type.
struct Cow<'a> {
x: &'a str,
}
//~v ERROR: could be elided: 'a
fn out_return_type_lts(e: &str) -> Cow<'_> {
unimplemented!()
}
mod issue2944 {
trait Foo {}
struct Bar;
struct Baz<'a> {
bar: &'a Bar,
}
//~v ERROR: could be elided: 'a
impl Foo for Baz<'_> {}
impl Bar {
//~v ERROR: could be elided: 'a
fn baz(&self) -> impl Foo + '_ {
Baz { bar: self }
}
}
}
mod issue13923 {
struct Py<'py> {
data: &'py str,
}
enum Content<'t, 'py> {
Py(Py<'py>),
T1(&'t str),
T2(&'t str),
}
enum ContentString<'t> {
T1(&'t str),
T2(&'t str),
}
impl<'t, 'py> ContentString<'t> {
// `'py` cannot be elided
fn map_content1(self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> {
match self {
Self::T1(content) => Content::T1(f(content)),
Self::T2(content) => Content::T2(f(content)),
}
}
}
//~v ERROR: could be elided: 'py
impl<'t> ContentString<'t> {
// `'py` can be elided because of `&self`
fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> {
match self {
Self::T1(content) => Content::T1(f(content)),
Self::T2(content) => Content::T2(f(content)),
}
}
}
//~v ERROR: could be elided: 'py
impl<'t> ContentString<'t> {
// `'py` can be elided because of `&'_ self`
fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> {
match self {
Self::T1(content) => Content::T1(f(content)),
Self::T2(content) => Content::T2(f(content)),
}
}
}
impl<'t, 'py> ContentString<'t> {
// `'py` should not be elided as the default lifetime, even if working, could be named as `'t`
fn map_content4(self, f: impl FnOnce(&'t str) -> &'t str, o: &'t str) -> Content<'t, 'py> {
match self {
Self::T1(content) => Content::T1(f(content)),
Self::T2(_) => Content::T2(o),
}
}
}
//~v ERROR: could be elided: 'py
impl<'t> ContentString<'t> {
// `'py` can be elided because of `&Self`
fn map_content5(
self: std::pin::Pin<&Self>,
f: impl FnOnce(&'t str) -> &'t str,
o: &'t str,
) -> Content<'t, '_> {
match *self {
Self::T1(content) => Content::T1(f(content)),
Self::T2(_) => Content::T2(o),
}
}
}
struct Cx<'a, 'b> {
a: &'a u32,
b: &'b u32,
}
// `'c` cannot be elided because we have several input lifetimes
fn one_explicit<'b>(x: Cx<'_, 'b>) -> &'b u32 {
x.b
}
}
fn issue15666_original() {
struct UnitVariantAccess<'a, 'b, 's>(&'a &'b &'s ());
trait Trait<'de> {}
//~v elidable_lifetime_names
impl<'de> Trait<'de> for UnitVariantAccess<'_, 'de, '_> {}
// ^^ ^^ ^^ ^^
}
#[allow(clippy::upper_case_acronyms)]
fn issue15666() {
struct S1<'a>(&'a ());
struct S2<'a, 'b>(&'a &'b ());
struct S3<'a, 'b, 'c>(&'a &'b &'c ());
trait T {}
trait TA<'a> {}
trait TB<'b> {}
trait TC<'c> {}
trait TAB<'a, 'b> {}
trait TAC<'a, 'c> {}
trait TBC<'b, 'c> {}
trait TABC<'a, 'b, 'c> {}
// 1 lifetime
impl<'a> TA<'a> for S1<'a> {}
//~v elidable_lifetime_names
impl T for S1<'_> {}
// ^^
// 2 lifetimes
impl<'a, 'b> TAB<'a, 'b> for S2<'a, 'b> {}
//~v elidable_lifetime_names
impl<'a> TA<'a> for S2<'a, '_> {}
// ^^
//~v elidable_lifetime_names
impl<'b> TB<'b> for S2<'_, 'b> {}
// ^^
//~v elidable_lifetime_names
impl T for S2<'_, '_> {}
// ^^ ^^
// 3 lifetimes
impl<'a, 'b, 'c> TABC<'a, 'b, 'c> for S3<'a, 'b, 'c> {}
//~v elidable_lifetime_names
impl<'a, 'b> TAB<'a, 'b> for S3<'a, 'b, '_> {}
// ^^
//~v elidable_lifetime_names
impl<'a, 'c> TAC<'a, 'c> for S3<'a, '_, 'c> {}
// ^^
//~v elidable_lifetime_names
impl<'a> TA<'a> for S3<'a, '_, '_> {}
// ^^ ^^
//~v elidable_lifetime_names
impl<'b, 'c> TBC<'b, 'c> for S3<'_, 'b, 'c> {}
// ^^
//~v elidable_lifetime_names
impl<'b> TB<'b> for S3<'_, 'b, '_> {}
// ^^ ^^
//~v elidable_lifetime_names
impl<'c> TC<'c> for S3<'_, '_, 'c> {}
// ^^ ^^
//~v elidable_lifetime_names
impl T for S3<'_, '_, '_> {}
// ^^ ^^ ^^
}
|