1 #![warn(clippy::needless_lifetimes)]
2 #![allow(dead_code, clippy::needless_pass_by_value, clippy::unnecessary_wraps, dyn_drop)]
4 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
6 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
8 // No error; same lifetime on two params.
9 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {}
11 // No error; static involved.
12 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {}
14 fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
16 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
20 // No error; multiple input refs.
21 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
25 // No error; multiple input refs.
26 fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
30 // No error; static involved.
31 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
36 fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
40 // No error; two input refs.
41 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
45 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
49 // Where-clause, but without lifetimes.
50 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
57 type Ref<'r> = &'r u8;
59 // No error; same lifetime on two params.
60 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
62 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
64 // No error; bounded lifetime.
65 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {}
67 // No error; bounded lifetime.
68 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
74 struct Lt<'a, I: 'static> {
78 // No error; fn bound references `'a`.
79 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
81 F: Fn(Lt<'a, I>) -> Lt<'a, I>,
86 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
88 for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
93 // No error; see below.
94 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
98 fn fn_bound_3_cannot_elide() {
102 // This will fail if we elide lifetimes of `fn_bound_3`.
103 fn_bound_3(p, |y| q = y);
106 // No error; multiple input refs.
107 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
108 if cond { x } else { f() }
116 fn self_and_out<'s>(&'s self) -> &'s u8 {
120 // No error; multiple input refs.
121 fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
125 fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
127 // No error; same lifetimes on two params.
128 fn self_and_same_in<'s>(&'s self, _x: &'s u8) {}
131 struct Foo<'a>(&'a u8);
134 // No error; lifetime `'a` not defined in method.
135 fn self_shared_lifetime(&self, _: &'a u8) {}
136 // No error; bounds exist.
137 fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {}
140 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
144 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
148 // No warning; two input lifetimes (named on the reference, anonymous on `Foo`).
149 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
153 // No warning; two input lifetimes (anonymous on the reference, named on `Foo`).
154 fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
158 // No warning; two input lifetimes.
159 fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
163 trait WithLifetime<'a> {}
165 type WithLifetimeAlias<'a> = dyn WithLifetime<'a>;
167 // Should not warn because it won't build without the lifetime.
168 fn trait_obj_elided<'a>(_arg: &'a dyn WithLifetime) -> &'a str {
172 // Should warn because there is no lifetime on `Drop`, so this would be
173 // unambiguous if we elided the lifetime.
174 fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str {
178 type FooAlias<'a> = Foo<'a>;
180 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
184 // No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`).
185 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
189 // No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`).
190 fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
194 // No warning; two input lifetimes.
195 fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
199 fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
203 fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
207 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
210 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
214 // Don't warn on these; see issue #292.
215 fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
225 fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
230 trait LintContext<'a> {}
232 fn f<'a, T: LintContext<'a>>(_: &T) {}
234 fn test<'a>(x: &'a [u8]) -> u8 {
235 let y: &'a u8 = &x[5];
239 // Issue #3284: give hint regarding lifetime in return type.
243 fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
247 // Make sure we still warn on implementations
250 fn needless_lt<'a>(x: &'a u8) {}
253 impl BadTrait for () {
254 fn needless_lt<'a>(_x: &'a u8) {}
265 impl<'a> Foo for Baz<'a> {}
267 fn baz<'a>(&'a self) -> impl Foo + 'a {
273 mod nested_elision_sites {
276 // closure trait bounds subject to nested elision
277 // don't lint because they refer to outer lifetimes
278 fn trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
281 fn trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
284 fn trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
289 fn impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32 {
292 fn impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
296 fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
299 fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
304 fn generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
308 fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
313 fn where_clause_not_elidable<'a, T>(f: T) -> &'a i32
320 fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
328 fn pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32 {
331 fn pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
335 fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 {
340 fn nested_fn_pointer_1<'a>(_: &'a i32) -> fn(fn(&'a i32) -> &'a i32) -> i32 {
343 fn nested_fn_pointer_2<'a>(_: &'a i32) -> impl Fn(fn(&'a i32)) {
348 fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 {
351 fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) {
358 pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
361 F: FnOnce(&'a T::Target) -> R,