fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
13.0 KiB546 linesraw
1//@aux-build:proc_macros.rs
2
3#![warn(clippy::needless_lifetimes)]
4#![allow(
5    unused,
6    clippy::boxed_local,
7    clippy::extra_unused_type_parameters,
8    clippy::needless_pass_by_value,
9    clippy::unnecessary_wraps,
10    dyn_drop,
11    clippy::get_first
12)]
13
14extern crate proc_macros;
15use proc_macros::inline_macros;
16
17fn distinct_lifetimes(_x: &u8, _y: &u8, _z: u8) {}
18
19fn distinct_and_static(_x: &u8, _y: &u8, _z: &'static u8) {}
20
21// No error; same lifetime on two params.
22fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {}
23
24// No error; static involved.
25fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {}
26
27fn mut_and_static_input(_x: &mut u8, _y: &'static str) {}
28
29fn in_and_out(x: &u8, _y: u8) -> &u8 {
30    x
31}
32
33// No error; multiple input refs.
34fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
35    x
36}
37
38// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
39//   fn multiple_in_and_out_2a<'a>(x: &'a u8, _y: &u8) -> &'a u8
40//                                                ^^^
41fn multiple_in_and_out_2a<'a>(x: &'a u8, _y: &u8) -> &'a u8 {
42    x
43}
44
45// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
46//   fn multiple_in_and_out_2b<'b>(_x: &u8, y: &'b u8) -> &'b u8
47//                                     ^^^
48fn multiple_in_and_out_2b<'b>(_x: &u8, y: &'b u8) -> &'b u8 {
49    y
50}
51
52// No error; multiple input refs
53async fn func<'a>(args: &[&'a str]) -> Option<&'a str> {
54    args.get(0).cloned()
55}
56
57// No error; static involved.
58fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
59    x
60}
61
62// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
63//   fn deep_reference_1a<'a>(x: &'a u8, _y: &u8) -> Result<&'a u8, ()>
64//                                           ^^^
65fn deep_reference_1a<'a>(x: &'a u8, _y: &u8) -> Result<&'a u8, ()> {
66    Ok(x)
67}
68
69// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
70//   fn deep_reference_1b<'b>(_x: &u8, y: &'b u8) -> Result<&'b u8, ()>
71//                                ^^^
72fn deep_reference_1b<'b>(_x: &u8, y: &'b u8) -> Result<&'b u8, ()> {
73    Ok(y)
74}
75
76// No error; two input refs.
77fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
78    x.unwrap()
79}
80
81fn deep_reference_3(x: &u8, _y: u8) -> Result<&u8, ()> {
82    Ok(x)
83}
84
85// Where-clause, but without lifetimes.
86fn where_clause_without_lt<T>(x: &u8, _y: u8) -> Result<&u8, ()>
87where
88    T: Copy,
89{
90    Ok(x)
91}
92
93type Ref<'r> = &'r u8;
94
95// No error; same lifetime on two params.
96fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
97
98fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {}
99
100// No error; bounded lifetime.
101fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {}
102
103// No error; bounded lifetime.
104fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
105where
106    'b: 'a,
107{
108}
109
110struct Lt<'a, I: 'static> {
111    x: &'a I,
112}
113
114// No error; fn bound references `'a`.
115fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
116where
117    F: Fn(Lt<'a, I>) -> Lt<'a, I>,
118{
119    unreachable!()
120}
121
122fn fn_bound_2<F, I>(_m: Lt<'_, I>, _f: F) -> Lt<'_, I>
123where
124    for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
125{
126    unreachable!()
127}
128
129// No error; see below.
130fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
131    f(x);
132}
133
134fn fn_bound_3_cannot_elide() {
135    let x = 42;
136    let p = &x;
137    let mut q = &x;
138    // This will fail if we elide lifetimes of `fn_bound_3`.
139    fn_bound_3(p, |y| q = y);
140}
141
142// No error; multiple input refs.
143fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
144    if cond { x } else { f() }
145}
146
147struct X {
148    x: u8,
149}
150
151impl X {
152    fn self_and_out(&self) -> &u8 {
153        &self.x
154    }
155
156    // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
157    //   fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8
158    //                                          ^^^
159    fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8 {
160        &self.x
161    }
162
163    // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
164    //   fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8
165    //                            ^^^^^
166    fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8 {
167        x
168    }
169
170    fn distinct_self_and_in(&self, _x: &u8) {}
171
172    // No error; same lifetimes on two params.
173    fn self_and_same_in<'s>(&'s self, _x: &'s u8) {}
174}
175
176struct Foo<'a>(&'a u8);
177
178impl<'a> Foo<'a> {
179    // No error; lifetime `'a` not defined in method.
180    fn self_shared_lifetime(&self, _: &'a u8) {}
181    // No error; bounds exist.
182    fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {}
183}
184
185fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
186    unimplemented!()
187}
188
189fn struct_with_lt(_foo: Foo<'_>) -> &str {
190    unimplemented!()
191}
192
193// No warning; two input lifetimes (named on the reference, anonymous on `Foo`).
194fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
195    unimplemented!()
196}
197
198// No warning; two input lifetimes (anonymous on the reference, named on `Foo`).
199fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
200    unimplemented!()
201}
202
203// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
204// valid:
205//   fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str
206//                                         ^^
207fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str {
208    unimplemented!()
209}
210
211// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
212// valid:
213//   fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str
214//                                 ^^^^
215fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str {
216    unimplemented!()
217}
218
219trait WithLifetime<'a> {}
220
221type WithLifetimeAlias<'a> = dyn WithLifetime<'a>;
222
223// Should not warn because it won't build without the lifetime.
224fn trait_obj_elided<'a>(_arg: &'a dyn WithLifetime) -> &'a str {
225    unimplemented!()
226}
227
228// Should warn because there is no lifetime on `Drop`, so this would be
229// unambiguous if we elided the lifetime.
230fn trait_obj_elided2(_arg: &dyn Drop) -> &str {
231    unimplemented!()
232}
233
234type FooAlias<'a> = Foo<'a>;
235
236fn alias_with_lt(_foo: FooAlias<'_>) -> &str {
237    unimplemented!()
238}
239
240// No warning; two input lifetimes (named on the reference, anonymous on `FooAlias`).
241fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
242    unimplemented!()
243}
244
245// No warning; two input lifetimes (anonymous on the reference, named on `FooAlias`).
246fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
247    unimplemented!()
248}
249
250// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
251// valid:
252//   fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str
253//                                             ^^
254fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str {
255    unimplemented!()
256}
257
258// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
259// valid:
260//   fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str
261//                                ^^^^^^^^^
262fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str {
263    unimplemented!()
264}
265
266fn named_input_elided_output(_arg: &str) -> &str {
267    unimplemented!()
268}
269
270fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
271    unimplemented!()
272}
273
274fn trait_bound_ok<T: WithLifetime<'static>>(_: &u8, _: T) {
275    unimplemented!()
276}
277fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
278    unimplemented!()
279}
280
281// Don't warn on these; see issue #292.
282fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
283    unimplemented!()
284}
285
286// See issue #740.
287struct Test {
288    vec: Vec<usize>,
289}
290
291impl Test {
292    fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
293        unimplemented!()
294    }
295}
296
297trait LintContext<'a> {}
298
299fn f<'a, T: LintContext<'a>>(_: &T) {}
300
301fn test<'a>(x: &'a [u8]) -> u8 {
302    let y: &'a u8 = &x[5];
303    *y
304}
305
306// Issue #3284: give hint regarding lifetime in return type.
307struct Cow<'a> {
308    x: &'a str,
309}
310fn out_return_type_lts(e: &str) -> Cow<'_> {
311    unimplemented!()
312}
313
314// Make sure we still warn on implementations
315mod issue4291 {
316    trait BadTrait {
317        fn needless_lt(x: &u8) {}
318    }
319
320    impl BadTrait for () {
321        fn needless_lt(_x: &u8) {}
322    }
323}
324
325mod issue2944 {
326    trait Foo {}
327    struct Bar;
328    struct Baz<'a> {
329        bar: &'a Bar,
330    }
331
332    impl<'a> Foo for Baz<'a> {}
333    impl Bar {
334        fn baz(&self) -> impl Foo + '_ {
335            Baz { bar: self }
336        }
337    }
338}
339
340mod nested_elision_sites {
341    // issue #issue2944
342
343    // closure trait bounds subject to nested elision
344    // don't lint because they refer to outer lifetimes
345    fn trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
346        move || i
347    }
348    fn trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
349        move || i
350    }
351    fn trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
352        move || i
353    }
354
355    // don't lint
356    fn impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32 {
357        f()
358    }
359    fn impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
360        move || i
361    }
362    // lint
363    fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
364        f(i)
365    }
366    fn impl_trait_elidable_nested_anonymous_lifetimes(i: &i32, f: impl Fn(&i32) -> &i32) -> &i32 {
367        f(i)
368    }
369
370    // don't lint
371    fn generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
372        f()
373    }
374    // lint
375    fn generics_elidable<T: Fn(&i32) -> &i32>(i: &i32, f: T) -> &i32 {
376        f(i)
377    }
378
379    // don't lint
380    fn where_clause_not_elidable<'a, T>(f: T) -> &'a i32
381    where
382        T: Fn() -> &'a i32,
383    {
384        f()
385    }
386    // lint
387    fn where_clause_elidadable<T>(i: &i32, f: T) -> &i32
388    where
389        T: Fn(&i32) -> &i32,
390    {
391        f(i)
392    }
393
394    // don't lint
395    fn pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32 {
396        f(i)
397    }
398    fn pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
399        |i| i
400    }
401    // lint
402    fn pointer_fn_elidable(i: &i32, f: fn(&i32) -> &i32) -> &i32 {
403        f(i)
404    }
405
406    // don't lint
407    fn nested_fn_pointer_1<'a>(_: &'a i32) -> fn(fn(&'a i32) -> &'a i32) -> i32 {
408        |f| 42
409    }
410    fn nested_fn_pointer_2<'a>(_: &'a i32) -> impl Fn(fn(&'a i32)) {
411        |f| ()
412    }
413
414    // lint
415    fn nested_fn_pointer_3(_: &i32) -> fn(fn(&i32) -> &i32) -> i32 {
416        |f| 42
417    }
418    fn nested_fn_pointer_4(_: &i32) -> impl Fn(fn(&i32)) {
419        |f| ()
420    }
421}
422
423mod issue6159 {
424    use std::ops::Deref;
425    pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
426    where
427        T: Deref,
428        F: FnOnce(&'a T::Target) -> R,
429    {
430        f(x.deref())
431    }
432}
433
434mod issue7296 {
435    use std::rc::Rc;
436    use std::sync::Arc;
437
438    struct Foo;
439    impl Foo {
440        fn implicit(&self) -> &() {
441            &()
442        }
443        fn implicit_mut(&mut self) -> &() {
444            &()
445        }
446
447        fn explicit<'a>(self: &'a Arc<Self>) -> &'a () {
448            &()
449        }
450        fn explicit_mut<'a>(self: &'a mut Rc<Self>) -> &'a () {
451            &()
452        }
453
454        fn lifetime_elsewhere(self: Box<Self>, here: &()) -> &() {
455            &()
456        }
457    }
458
459    trait Bar {
460        fn implicit(&self) -> &();
461        fn implicit_provided(&self) -> &() {
462            &()
463        }
464
465        fn explicit<'a>(self: &'a Arc<Self>) -> &'a ();
466        fn explicit_provided<'a>(self: &'a Arc<Self>) -> &'a () {
467            &()
468        }
469
470        fn lifetime_elsewhere(self: Box<Self>, here: &()) -> &();
471        fn lifetime_elsewhere_provided(self: Box<Self>, here: &()) -> &() {
472            &()
473        }
474    }
475}
476
477mod pr_9743_false_negative_fix {
478    #![allow(unused)]
479
480    fn foo(x: &u8, y: &'_ u8) {}
481
482    fn bar(x: &u8, y: &'_ u8, z: &'_ u8) {}
483}
484
485mod pr_9743_output_lifetime_checks {
486    #![allow(unused)]
487
488    // lint: only one input
489    fn one_input(x: &u8) -> &u8 {
490        unimplemented!()
491    }
492
493    // lint: multiple inputs, output would not be elided
494    fn multiple_inputs_output_not_elided<'b>(x: &u8, y: &'b u8, z: &'b u8) -> &'b u8 {
495        unimplemented!()
496    }
497
498    // don't lint: multiple inputs, output would be elided (which would create an ambiguity)
499    fn multiple_inputs_output_would_be_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'a u8 {
500        unimplemented!()
501    }
502}
503
504#[inline_macros]
505mod in_macro {
506    use proc_macros::external;
507
508    // lint local macro expands to function with needless lifetimes
509    inline! {
510        fn one_input(x: &u8) -> &u8 {
511            unimplemented!()
512        }
513    }
514
515    // no lint on external macro
516    external! {
517        fn needless_lifetime<'a>(x: &'a u8) -> &'a u8 {
518            unimplemented!()
519        }
520    }
521
522    inline! {
523        fn f<$'a>(arg: &$'a str) -> &$'a str {
524            arg
525        }
526    }
527}
528
529mod issue5787 {
530    use std::sync::MutexGuard;
531
532    struct Foo;
533
534    impl Foo {
535        // doesn't get linted without async
536        pub async fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
537            guard
538        }
539    }
540
541    async fn foo<'a>(_x: &i32, y: &'a str) -> &'a str {
542        y
543    }
544}
545
546fn main() {}