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