]> git.lizzy.rs Git - rust.git/blob - src/test/ui/in-band-lifetimes/elided-lifetimes.fixed
Do not suggest using a const parameter when there are bounds on an unused type parameter
[rust.git] / src / test / ui / in-band-lifetimes / elided-lifetimes.fixed
1 // run-rustfix
2 // edition:2018
3
4 #![allow(unused)]
5 #![deny(elided_lifetimes_in_paths)]
6 //~^ NOTE the lint level is defined here
7
8 use std::cell::{Ref, RefCell};
9
10 struct Foo<'a> {
11     x: &'a u32,
12 }
13
14 fn foo(x: &Foo<'_>) {
15     //~^ ERROR hidden lifetime parameters in types are deprecated
16     //~| NOTE expected named lifetime parameter
17     //~| HELP consider using the `'_` lifetime
18 }
19
20 fn bar(x: &Foo<'_>) {}
21
22 struct Wrapped<'a>(&'a str);
23
24 struct WrappedWithBow<'a> {
25     gift: &'a str,
26 }
27
28 struct MatchedSet<'a, 'b> {
29     one: &'a str,
30     another: &'b str,
31 }
32
33 fn wrap_gift(gift: &str) -> Wrapped<'_> {
34     //~^ ERROR hidden lifetime parameters in types are deprecated
35     //~| NOTE expected named lifetime parameter
36     //~| HELP consider using the `'_` lifetime
37     Wrapped(gift)
38 }
39
40 fn wrap_gift_with_bow(gift: &str) -> WrappedWithBow<'_> {
41     //~^ ERROR hidden lifetime parameters in types are deprecated
42     //~| NOTE expected named lifetime parameter
43     //~| HELP consider using the `'_` lifetime
44     WrappedWithBow { gift }
45 }
46
47 fn inspect_matched_set(set: MatchedSet<'_, '_>) {
48     //~^ ERROR hidden lifetime parameters in types are deprecated
49     //~| NOTE expected 2 lifetime parameters
50     //~| HELP consider using the `'_` lifetime
51     println!("{} {}", set.one, set.another);
52 }
53
54 // Verify that the lint does not fire, because the added `'_` wouldn't be resolved correctly.
55 fn match_sets() -> MatchedSet<'static, 'static> {
56     //~^ ERROR missing lifetime specifiers
57     //~| NOTE expected 2 lifetime parameters
58     //~| HELP this function's return type contains a borrowed value
59     //~| HELP consider using the `'static` lifetime
60     MatchedSet { one: "one", another: "another" }
61 }
62
63 macro_rules! autowrapper {
64     ($type_name:ident, $fn_name:ident, $lt:lifetime) => {
65         struct $type_name<$lt> {
66             gift: &$lt str
67         }
68
69         fn $fn_name(gift: &str) -> $type_name<'_> {
70             //~^ ERROR hidden lifetime parameters in types are deprecated
71             //~| NOTE expected named lifetime parameter
72             //~| HELP consider using the `'_` lifetime
73             //~| ERROR hidden lifetime parameters in types are deprecated
74             //~| NOTE expected named lifetime parameter
75             //~| HELP consider using the `'_` lifetime
76             $type_name { gift }
77         }
78     }
79 }
80
81 autowrapper!(Autowrapped, autowrap_gift, 'a);
82 //~^ NOTE in this expansion of autowrapper!
83 //~| NOTE in this expansion of autowrapper!
84
85 // Verify that rustfix does not try to apply the fix twice.
86 autowrapper!(AutowrappedAgain, autowrap_gift_again, 'a);
87 //~^ NOTE in this expansion of autowrapper!
88 //~| NOTE in this expansion of autowrapper!
89
90 macro_rules! anytuple_ref_ty {
91     ($($types:ty),*) => {
92         Ref<'_, ($($types),*)>
93         //~^ ERROR hidden lifetime parameters in types are deprecated
94         //~| NOTE expected named lifetime parameter
95         //~| HELP consider using the `'_` lifetime
96     }
97 }
98
99 #[allow(elided_lifetimes_in_paths)]
100 mod blah {
101     struct Thing<'a>(&'a i32);
102     struct Bar<T>(T);
103
104     fn foo(b: Bar<Thing>) {}
105 }
106
107 fn main() {
108     let honesty = RefCell::new((4, 'e'));
109     let loyalty: Ref<'_, (u32, char)> = honesty.borrow();
110     //~^ ERROR hidden lifetime parameters in types are deprecated
111     //~| NOTE expected named lifetime parameter
112     //~| HELP consider using the `'_` lifetime
113     let generosity = Ref::map(loyalty, |t| &t.0);
114
115     let laughter = RefCell::new((true, "magic"));
116     let yellow: anytuple_ref_ty!(bool, &str) = laughter.borrow();
117     //~^ NOTE in this expansion of anytuple_ref_ty!
118     //~| NOTE in this expansion of anytuple_ref_ty!
119 }