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