]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_lifetimes.rs
Rollup merge of #97149 - ChrisDenton:win_async_pipes, r=m-ou-se
[rust.git] / src / tools / clippy / tests / ui / needless_lifetimes.rs
1 #![warn(clippy::needless_lifetimes)]
2 #![allow(
3     dead_code,
4     clippy::boxed_local,
5     clippy::needless_pass_by_value,
6     clippy::unnecessary_wraps,
7     dyn_drop,
8     clippy::get_first
9 )]
10
11 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
12
13 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
14
15 // No error; same lifetime on two params.
16 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {}
17
18 // No error; static involved.
19 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {}
20
21 fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
22
23 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
24     x
25 }
26
27 // No error; multiple input refs.
28 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
29     x
30 }
31
32 // No error; multiple input refs.
33 fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
34     x
35 }
36
37 // No error; multiple input refs
38 async fn func<'a>(args: &[&'a str]) -> Option<&'a str> {
39     args.get(0).cloned()
40 }
41
42 // No error; static involved.
43 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
44     x
45 }
46
47 // No error.
48 fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
49     Ok(x)
50 }
51
52 // No error; two input refs.
53 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
54     x.unwrap()
55 }
56
57 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
58     Ok(x)
59 }
60
61 // Where-clause, but without lifetimes.
62 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
63 where
64     T: Copy,
65 {
66     Ok(x)
67 }
68
69 type Ref<'r> = &'r u8;
70
71 // No error; same lifetime on two params.
72 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
73
74 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
75
76 // No error; bounded lifetime.
77 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {}
78
79 // No error; bounded lifetime.
80 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
81 where
82     'b: 'a,
83 {
84 }
85
86 struct Lt<'a, I: 'static> {
87     x: &'a I,
88 }
89
90 // No error; fn bound references `'a`.
91 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
92 where
93     F: Fn(Lt<'a, I>) -> Lt<'a, I>,
94 {
95     unreachable!()
96 }
97
98 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
99 where
100     for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
101 {
102     unreachable!()
103 }
104
105 // No error; see below.
106 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
107     f(x);
108 }
109
110 fn fn_bound_3_cannot_elide() {
111     let x = 42;
112     let p = &x;
113     let mut q = &x;
114     // This will fail if we elide lifetimes of `fn_bound_3`.
115     fn_bound_3(p, |y| q = y);
116 }
117
118 // No error; multiple input refs.
119 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
120     if cond { x } else { f() }
121 }
122
123 struct X {
124     x: u8,
125 }
126
127 impl X {
128     fn self_and_out<'s>(&'s self) -> &'s u8 {
129         &self.x
130     }
131
132     // No error; multiple input refs.
133     fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
134         &self.x
135     }
136
137     fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
138
139     // No error; same lifetimes on two params.
140     fn self_and_same_in<'s>(&'s self, _x: &'s u8) {}
141 }
142
143 struct Foo<'a>(&'a u8);
144
145 impl<'a> Foo<'a> {
146     // No error; lifetime `'a` not defined in method.
147     fn self_shared_lifetime(&self, _: &'a u8) {}
148     // No error; bounds exist.
149     fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {}
150 }
151
152 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
153     unimplemented!()
154 }
155
156 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
157     unimplemented!()
158 }
159
160 // No warning; two input lifetimes (named on the reference, anonymous on `Foo`).
161 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
162     unimplemented!()
163 }
164
165 // No warning; two input lifetimes (anonymous on the reference, named on `Foo`).
166 fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
167     unimplemented!()
168 }
169
170 // No warning; two input lifetimes.
171 fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
172     unimplemented!()
173 }
174
175 trait WithLifetime<'a> {}
176
177 type WithLifetimeAlias<'a> = dyn WithLifetime<'a>;
178
179 // Should not warn because it won't build without the lifetime.
180 fn trait_obj_elided<'a>(_arg: &'a dyn WithLifetime) -> &'a str {
181     unimplemented!()
182 }
183
184 // Should warn because there is no lifetime on `Drop`, so this would be
185 // unambiguous if we elided the lifetime.
186 fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str {
187     unimplemented!()
188 }
189
190 type FooAlias<'a> = Foo<'a>;
191
192 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
193     unimplemented!()
194 }
195
196 // No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`).
197 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
198     unimplemented!()
199 }
200
201 // No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`).
202 fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
203     unimplemented!()
204 }
205
206 // No warning; two input lifetimes.
207 fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
208     unimplemented!()
209 }
210
211 fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
212     unimplemented!()
213 }
214
215 fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
216     unimplemented!()
217 }
218
219 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
220     unimplemented!()
221 }
222 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
223     unimplemented!()
224 }
225
226 // Don't warn on these; see issue #292.
227 fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
228     unimplemented!()
229 }
230
231 // See issue #740.
232 struct Test {
233     vec: Vec<usize>,
234 }
235
236 impl Test {
237     fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
238         unimplemented!()
239     }
240 }
241
242 trait LintContext<'a> {}
243
244 fn f<'a, T: LintContext<'a>>(_: &T) {}
245
246 fn test<'a>(x: &'a [u8]) -> u8 {
247     let y: &'a u8 = &x[5];
248     *y
249 }
250
251 // Issue #3284: give hint regarding lifetime in return type.
252 struct Cow<'a> {
253     x: &'a str,
254 }
255 fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
256     unimplemented!()
257 }
258
259 // Make sure we still warn on implementations
260 mod issue4291 {
261     trait BadTrait {
262         fn needless_lt<'a>(x: &'a u8) {}
263     }
264
265     impl BadTrait for () {
266         fn needless_lt<'a>(_x: &'a u8) {}
267     }
268 }
269
270 mod issue2944 {
271     trait Foo {}
272     struct Bar;
273     struct Baz<'a> {
274         bar: &'a Bar,
275     }
276
277     impl<'a> Foo for Baz<'a> {}
278     impl Bar {
279         fn baz<'a>(&'a self) -> impl Foo + 'a {
280             Baz { bar: self }
281         }
282     }
283 }
284
285 mod nested_elision_sites {
286     // issue #issue2944
287
288     // closure trait bounds subject to nested elision
289     // don't lint because they refer to outer lifetimes
290     fn trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
291         move || i
292     }
293     fn trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
294         move || i
295     }
296     fn trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
297         move || i
298     }
299
300     // don't lint
301     fn impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32 {
302         f()
303     }
304     fn impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
305         move || i
306     }
307     // lint
308     fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
309         f(i)
310     }
311     fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
312         f(i)
313     }
314
315     // don't lint
316     fn generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
317         f()
318     }
319     // lint
320     fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
321         f(i)
322     }
323
324     // don't lint
325     fn where_clause_not_elidable<'a, T>(f: T) -> &'a i32
326     where
327         T: Fn() -> &'a i32,
328     {
329         f()
330     }
331     // lint
332     fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
333     where
334         T: Fn(&i32) -> &i32,
335     {
336         f(i)
337     }
338
339     // don't lint
340     fn pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32 {
341         f(i)
342     }
343     fn pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
344         |i| i
345     }
346     // lint
347     fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 {
348         f(i)
349     }
350
351     // don't lint
352     fn nested_fn_pointer_1<'a>(_: &'a i32) -> fn(fn(&'a i32) -> &'a i32) -> i32 {
353         |f| 42
354     }
355     fn nested_fn_pointer_2<'a>(_: &'a i32) -> impl Fn(fn(&'a i32)) {
356         |f| ()
357     }
358
359     // lint
360     fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 {
361         |f| 42
362     }
363     fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) {
364         |f| ()
365     }
366 }
367
368 mod issue6159 {
369     use std::ops::Deref;
370     pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
371     where
372         T: Deref,
373         F: FnOnce(&'a T::Target) -> R,
374     {
375         f(x.deref())
376     }
377 }
378
379 mod issue7296 {
380     use std::rc::Rc;
381     use std::sync::Arc;
382
383     struct Foo;
384     impl Foo {
385         fn implicit<'a>(&'a self) -> &'a () {
386             &()
387         }
388         fn implicit_mut<'a>(&'a mut self) -> &'a () {
389             &()
390         }
391
392         fn explicit<'a>(self: &'a Arc<Self>) -> &'a () {
393             &()
394         }
395         fn explicit_mut<'a>(self: &'a mut Rc<Self>) -> &'a () {
396             &()
397         }
398
399         fn lifetime_elsewhere<'a>(self: Box<Self>, here: &'a ()) -> &'a () {
400             &()
401         }
402     }
403
404     trait Bar {
405         fn implicit<'a>(&'a self) -> &'a ();
406         fn implicit_provided<'a>(&'a self) -> &'a () {
407             &()
408         }
409
410         fn explicit<'a>(self: &'a Arc<Self>) -> &'a ();
411         fn explicit_provided<'a>(self: &'a Arc<Self>) -> &'a () {
412             &()
413         }
414
415         fn lifetime_elsewhere<'a>(self: Box<Self>, here: &'a ()) -> &'a ();
416         fn lifetime_elsewhere_provided<'a>(self: Box<Self>, here: &'a ()) -> &'a () {
417             &()
418         }
419     }
420 }
421
422 fn main() {}