1 #![warn(clippy::needless_lifetimes)]
5 clippy::needless_pass_by_value,
6 clippy::unnecessary_wraps,
11 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
13 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
15 // No error; same lifetime on two params.
16 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {}
18 // No error; static involved.
19 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {}
21 fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
23 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
27 // No error; multiple input refs.
28 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
32 // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
33 // fn multiple_in_and_out_2a<'a>(x: &'a u8, _y: &u8) -> &'a u8
35 fn multiple_in_and_out_2a<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
39 // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
40 // fn multiple_in_and_out_2b<'b>(_x: &u8, y: &'b u8) -> &'b u8
42 fn multiple_in_and_out_2b<'a, 'b>(_x: &'a u8, y: &'b u8) -> &'b u8 {
46 // No error; multiple input refs
47 async fn func<'a>(args: &[&'a str]) -> Option<&'a str> {
51 // No error; static involved.
52 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
56 // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
57 // fn deep_reference_1a<'a>(x: &'a u8, _y: &u8) -> Result<&'a u8, ()>
59 fn deep_reference_1a<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
63 // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
64 // fn deep_reference_1b<'b>(_x: &u8, y: &'b u8) -> Result<&'b u8, ()>
66 fn deep_reference_1b<'a, 'b>(_x: &'a u8, y: &'b u8) -> Result<&'b u8, ()> {
70 // No error; two input refs.
71 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
75 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
79 // Where-clause, but without lifetimes.
80 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
87 type Ref<'r> = &'r u8;
89 // No error; same lifetime on two params.
90 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
92 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
94 // No error; bounded lifetime.
95 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {}
97 // No error; bounded lifetime.
98 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
104 struct Lt<'a, I: 'static> {
108 // No error; fn bound references `'a`.
109 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
111 F: Fn(Lt<'a, I>) -> Lt<'a, I>,
116 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
118 for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
123 // No error; see below.
124 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
128 fn fn_bound_3_cannot_elide() {
132 // This will fail if we elide lifetimes of `fn_bound_3`.
133 fn_bound_3(p, |y| q = y);
136 // No error; multiple input refs.
137 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
138 if cond { x } else { f() }
146 fn self_and_out<'s>(&'s self) -> &'s u8 {
150 // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
151 // fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8
153 fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
157 // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
158 // fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8
160 fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 {
164 fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
166 // No error; same lifetimes on two params.
167 fn self_and_same_in<'s>(&'s self, _x: &'s u8) {}
170 struct Foo<'a>(&'a u8);
173 // No error; lifetime `'a` not defined in method.
174 fn self_shared_lifetime(&self, _: &'a u8) {}
175 // No error; bounds exist.
176 fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {}
179 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
183 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
187 // No warning; two input lifetimes (named on the reference, anonymous on `Foo`).
188 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
192 // No warning; two input lifetimes (anonymous on the reference, named on `Foo`).
193 fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
197 // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
199 // fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str
201 fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
205 // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
207 // fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str
209 fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str {
213 trait WithLifetime<'a> {}
215 type WithLifetimeAlias<'a> = dyn WithLifetime<'a>;
217 // Should not warn because it won't build without the lifetime.
218 fn trait_obj_elided<'a>(_arg: &'a dyn WithLifetime) -> &'a str {
222 // Should warn because there is no lifetime on `Drop`, so this would be
223 // unambiguous if we elided the lifetime.
224 fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str {
228 type FooAlias<'a> = Foo<'a>;
230 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
234 // No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`).
235 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
239 // No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`).
240 fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
244 // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
246 // fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str
248 fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
252 // Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
254 // fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str
256 fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str {
260 fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
264 fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
268 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
271 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
275 // Don't warn on these; see issue #292.
276 fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
286 fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
291 trait LintContext<'a> {}
293 fn f<'a, T: LintContext<'a>>(_: &T) {}
295 fn test<'a>(x: &'a [u8]) -> u8 {
296 let y: &'a u8 = &x[5];
300 // Issue #3284: give hint regarding lifetime in return type.
304 fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
308 // Make sure we still warn on implementations
311 fn needless_lt<'a>(x: &'a u8) {}
314 impl BadTrait for () {
315 fn needless_lt<'a>(_x: &'a u8) {}
326 impl<'a> Foo for Baz<'a> {}
328 fn baz<'a>(&'a self) -> impl Foo + 'a {
334 mod nested_elision_sites {
337 // closure trait bounds subject to nested elision
338 // don't lint because they refer to outer lifetimes
339 fn trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
342 fn trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
345 fn trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
350 fn impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32 {
353 fn impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
357 fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
360 fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
365 fn generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
369 fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
374 fn where_clause_not_elidable<'a, T>(f: T) -> &'a i32
381 fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
389 fn pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32 {
392 fn pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
396 fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 {
401 fn nested_fn_pointer_1<'a>(_: &'a i32) -> fn(fn(&'a i32) -> &'a i32) -> i32 {
404 fn nested_fn_pointer_2<'a>(_: &'a i32) -> impl Fn(fn(&'a i32)) {
409 fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 {
412 fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) {
419 pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
422 F: FnOnce(&'a T::Target) -> R,
434 fn implicit<'a>(&'a self) -> &'a () {
437 fn implicit_mut<'a>(&'a mut self) -> &'a () {
441 fn explicit<'a>(self: &'a Arc<Self>) -> &'a () {
444 fn explicit_mut<'a>(self: &'a mut Rc<Self>) -> &'a () {
448 fn lifetime_elsewhere<'a>(self: Box<Self>, here: &'a ()) -> &'a () {
454 fn implicit<'a>(&'a self) -> &'a ();
455 fn implicit_provided<'a>(&'a self) -> &'a () {
459 fn explicit<'a>(self: &'a Arc<Self>) -> &'a ();
460 fn explicit_provided<'a>(self: &'a Arc<Self>) -> &'a () {
464 fn lifetime_elsewhere<'a>(self: Box<Self>, here: &'a ()) -> &'a ();
465 fn lifetime_elsewhere_provided<'a>(self: Box<Self>, here: &'a ()) -> &'a () {
471 mod pr_9743_false_negative_fix {
474 fn foo<'a>(x: &'a u8, y: &'_ u8) {}
476 fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {}
479 mod pr_9743_output_lifetime_checks {
482 // lint: only one input
483 fn one_input<'a>(x: &'a u8) -> &'a u8 {
487 // lint: multiple inputs, output would not be elided
488 fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 {
492 // don't lint: multiple inputs, output would be elided (which would create an ambiguity)
493 fn multiple_inputs_output_would_be_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'a u8 {