1 #![warn(clippy::needless_lifetimes)]
2 #![allow(dead_code, clippy::needless_pass_by_value, clippy::unnecessary_wraps, dyn_drop)]
3 
distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8)4 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
5 
distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8)6 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
7 
8 // No error; same lifetime on two params.
same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8)9 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {}
10 
11 // No error; static involved.
only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8)12 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {}
13 
mut_and_static_input(_x: &mut u8, _y: &'static str)14 fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
15 
in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u816 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
17     x
18 }
19 
20 // No error; multiple input refs.
multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u821 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
22     x
23 }
24 
25 // No error; multiple input refs.
multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u826 fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
27     x
28 }
29 
30 // No error; multiple input refs
func<'a>(args: &[&'a str]) -> Option<&'a str>31 async fn func<'a>(args: &[&'a str]) -> Option<&'a str> {
32     args.get(0).cloned()
33 }
34 
35 // No error; static involved.
in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u836 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
37     x
38 }
39 
40 // No error.
deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()>41 fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
42     Ok(x)
43 }
44 
45 // No error; two input refs.
deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u846 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
47     x.unwrap()
48 }
49 
deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>50 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
51     Ok(x)
52 }
53 
54 // Where-clause, but without lifetimes.
where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> where T: Copy,55 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
56 where
57     T: Copy,
58 {
59     Ok(x)
60 }
61 
62 type Ref<'r> = &'r u8;
63 
64 // No error; same lifetime on two params.
lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8)65 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
66 
lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8)67 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
68 
69 // No error; bounded lifetime.
lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8)70 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {}
71 
72 // No error; bounded lifetime.
lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8) where 'b: 'a,73 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
74 where
75     'b: 'a,
76 {
77 }
78 
79 struct Lt<'a, I: 'static> {
80     x: &'a I,
81 }
82 
83 // No error; fn bound references `'a`.
fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> where F: Fn(Lt<'a, I>) -> Lt<'a, I>,84 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
85 where
86     F: Fn(Lt<'a, I>) -> Lt<'a, I>,
87 {
88     unreachable!()
89 }
90 
fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I> where for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,91 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
92 where
93     for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
94 {
95     unreachable!()
96 }
97 
98 // No error; see below.
fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F)99 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
100     f(x);
101 }
102 
fn_bound_3_cannot_elide()103 fn fn_bound_3_cannot_elide() {
104     let x = 42;
105     let p = &x;
106     let mut q = &x;
107     // This will fail if we elide lifetimes of `fn_bound_3`.
108     fn_bound_3(p, |y| q = y);
109 }
110 
111 // No error; multiple input refs.
fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a ()112 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
113     if cond { x } else { f() }
114 }
115 
116 struct X {
117     x: u8,
118 }
119 
120 impl X {
self_and_out<'s>(&'s self) -> &'s u8121     fn self_and_out<'s>(&'s self) -> &'s u8 {
122         &self.x
123     }
124 
125     // No error; multiple input refs.
self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8126     fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
127         &self.x
128     }
129 
distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8)130     fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
131 
132     // No error; same lifetimes on two params.
self_and_same_in<'s>(&'s self, _x: &'s u8)133     fn self_and_same_in<'s>(&'s self, _x: &'s u8) {}
134 }
135 
136 struct Foo<'a>(&'a u8);
137 
138 impl<'a> Foo<'a> {
139     // No error; lifetime `'a` not defined in method.
self_shared_lifetime(&self, _: &'a u8)140     fn self_shared_lifetime(&self, _: &'a u8) {}
141     // No error; bounds exist.
self_bound_lifetime<'b: 'a>(&self, _: &'b u8)142     fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {}
143 }
144 
already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8145 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
146     unimplemented!()
147 }
148 
struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str149 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
150     unimplemented!()
151 }
152 
153 // No warning; two input lifetimes (named on the reference, anonymous on `Foo`).
struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str154 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
155     unimplemented!()
156 }
157 
158 // No warning; two input lifetimes (anonymous on the reference, named on `Foo`).
struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str159 fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
160     unimplemented!()
161 }
162 
163 // No warning; two input lifetimes.
struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str164 fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
165     unimplemented!()
166 }
167 
168 trait WithLifetime<'a> {}
169 
170 type WithLifetimeAlias<'a> = dyn WithLifetime<'a>;
171 
172 // Should not warn because it won't build without the lifetime.
trait_obj_elided<'a>(_arg: &'a dyn WithLifetime) -> &'a str173 fn trait_obj_elided<'a>(_arg: &'a dyn WithLifetime) -> &'a str {
174     unimplemented!()
175 }
176 
177 // Should warn because there is no lifetime on `Drop`, so this would be
178 // unambiguous if we elided the lifetime.
trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str179 fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str {
180     unimplemented!()
181 }
182 
183 type FooAlias<'a> = Foo<'a>;
184 
alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str185 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
186     unimplemented!()
187 }
188 
189 // No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`).
alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str190 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
191     unimplemented!()
192 }
193 
194 // No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`).
alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str195 fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
196     unimplemented!()
197 }
198 
199 // No warning; two input lifetimes.
alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str200 fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
201     unimplemented!()
202 }
203 
named_input_elided_output<'a>(_arg: &'a str) -> &str204 fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
205     unimplemented!()
206 }
207 
elided_input_named_output<'a>(_arg: &str) -> &'a str208 fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
209     unimplemented!()
210 }
211 
trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T)212 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
213     unimplemented!()
214 }
trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T)215 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
216     unimplemented!()
217 }
218 
219 // Don't warn on these; see issue #292.
trait_bound_bug<'a, T: WithLifetime<'a>>()220 fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
221     unimplemented!()
222 }
223 
224 // See issue #740.
225 struct Test {
226     vec: Vec<usize>,
227 }
228 
229 impl Test {
iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a>230     fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
231         unimplemented!()
232     }
233 }
234 
235 trait LintContext<'a> {}
236 
f<'a, T: LintContext<'a>>(_: &T)237 fn f<'a, T: LintContext<'a>>(_: &T) {}
238 
test<'a>(x: &'a [u8]) -> u8239 fn test<'a>(x: &'a [u8]) -> u8 {
240     let y: &'a u8 = &x[5];
241     *y
242 }
243 
244 // Issue #3284: give hint regarding lifetime in return type.
245 struct Cow<'a> {
246     x: &'a str,
247 }
out_return_type_lts<'a>(e: &'a str) -> Cow<'a>248 fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
249     unimplemented!()
250 }
251 
252 // Make sure we still warn on implementations
253 mod issue4291 {
254     trait BadTrait {
needless_lt<'a>(x: &'a u8)255         fn needless_lt<'a>(x: &'a u8) {}
256     }
257 
258     impl BadTrait for () {
needless_lt<'a>(_x: &'a u8)259         fn needless_lt<'a>(_x: &'a u8) {}
260     }
261 }
262 
263 mod issue2944 {
264     trait Foo {}
265     struct Bar {}
266     struct Baz<'a> {
267         bar: &'a Bar,
268     }
269 
270     impl<'a> Foo for Baz<'a> {}
271     impl Bar {
baz<'a>(&'a self) -> impl Foo + 'a272         fn baz<'a>(&'a self) -> impl Foo + 'a {
273             Baz { bar: self }
274         }
275     }
276 }
277 
278 mod nested_elision_sites {
279     // issue #issue2944
280 
281     // closure trait bounds subject to nested elision
282     // don't lint because they refer to outer lifetimes
trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32283     fn trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
284         move || i
285     }
trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32286     fn trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
287         move || i
288     }
trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32289     fn trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
290         move || i
291     }
292 
293     // don't lint
impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32294     fn impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32 {
295         f()
296     }
impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32297     fn impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
298         move || i
299     }
300     // lint
impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32301     fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
302         f(i)
303     }
impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32304     fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
305         f(i)
306     }
307 
308     // don't lint
generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32309     fn generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
310         f()
311     }
312     // lint
generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32313     fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
314         f(i)
315     }
316 
317     // don't lint
where_clause_not_elidable<'a, T>(f: T) -> &'a i32 where T: Fn() -> &'a i32,318     fn where_clause_not_elidable<'a, T>(f: T) -> &'a i32
319     where
320         T: Fn() -> &'a i32,
321     {
322         f()
323     }
324     // lint
where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32 where T: Fn(&i32) -> &i32,325     fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
326     where
327         T: Fn(&i32) -> &i32,
328     {
329         f(i)
330     }
331 
332     // don't lint
pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32333     fn pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32 {
334         f(i)
335     }
pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32336     fn pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
337         |i| i
338     }
339     // lint
pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32340     fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 {
341         f(i)
342     }
343 
344     // don't lint
nested_fn_pointer_1<'a>(_: &'a i32) -> fn(fn(&'a i32) -> &'a i32) -> i32345     fn nested_fn_pointer_1<'a>(_: &'a i32) -> fn(fn(&'a i32) -> &'a i32) -> i32 {
346         |f| 42
347     }
nested_fn_pointer_2<'a>(_: &'a i32) -> impl Fn(fn(&'a i32))348     fn nested_fn_pointer_2<'a>(_: &'a i32) -> impl Fn(fn(&'a i32)) {
349         |f| ()
350     }
351 
352     // lint
nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32353     fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 {
354         |f| 42
355     }
nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32))356     fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) {
357         |f| ()
358     }
359 }
360 
361 mod issue6159 {
362     use std::ops::Deref;
apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R where T: Deref, F: FnOnce(&'a T::Target) -> R,363     pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
364     where
365         T: Deref,
366         F: FnOnce(&'a T::Target) -> R,
367     {
368         f(x.deref())
369     }
370 }
371 
main()372 fn main() {}
373