]> git.lizzy.rs Git - rust.git/blob - tests/ui/lifetimes.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / lifetimes.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::needless_lifetimes, clippy::extra_unused_lifetimes)]
11 #![allow(dead_code, clippy::needless_pass_by_value, clippy::trivially_copy_pass_by_ref)]
12
13 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
14
15 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
16
17 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) {} // no error, same lifetime on two params
18
19 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) {} // no error, static involved
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 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
28     x
29 } // no error, multiple input refs
30
31 fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
32     x
33 } // no error, multiple input refs
34
35 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
36     x
37 } // no error, static involved
38
39 fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
40     Ok(x)
41 } // no error
42
43 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
44     x.unwrap()
45 } // no error, two input refs
46
47 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
48     Ok(x)
49 }
50
51 // where clause, but without lifetimes
52 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
53 where
54     T: Copy,
55 {
56     Ok(x)
57 }
58
59 type Ref<'r> = &'r u8;
60
61 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {} // no error, same lifetime on two params
62
63 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
64
65 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) {} // no error, bounded lifetime
66
67 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8)
68 where
69     'b: 'a,
70 {
71 } // no error, bounded lifetime
72
73 struct Lt<'a, I: 'static> {
74     x: &'a I,
75 }
76
77 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
78 where
79     F: Fn(Lt<'a, I>) -> Lt<'a, I>, // no error, fn bound references 'a
80 {
81     unreachable!()
82 }
83
84 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
85 where
86     for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
87 {
88     unreachable!()
89 }
90
91 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) {
92     // no error, see below
93     f(x);
94 }
95
96 fn fn_bound_3_cannot_elide() {
97     let x = 42;
98     let p = &x;
99     let mut q = &x;
100     fn_bound_3(p, |y| q = y); // this will fail if we elides lifetimes of `fn_bound_3`
101 }
102
103 // no error, multiple input refs
104 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
105     if cond {
106         x
107     } else {
108         f()
109     }
110 }
111
112 struct X {
113     x: u8,
114 }
115
116 impl X {
117     fn self_and_out<'s>(&'s self) -> &'s u8 {
118         &self.x
119     }
120
121     fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
122         &self.x
123     } // no error, multiple input refs
124
125     fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
126
127     fn self_and_same_in<'s>(&'s self, _x: &'s u8) {} // no error, same lifetimes on two params
128 }
129
130 struct Foo<'a>(&'a u8);
131
132 impl<'a> Foo<'a> {
133     fn self_shared_lifetime(&self, _: &'a u8) {} // no error, lifetime 'a not defined in method
134     fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {} // no error, bounds exist
135 }
136
137 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
138     unimplemented!()
139 }
140
141 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
142     unimplemented!()
143 }
144
145 // no warning, two input lifetimes (named on the reference, anonymous on Foo)
146 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str {
147     unimplemented!()
148 }
149
150 // no warning, two input lifetimes (anonymous on the reference, named on Foo)
151 fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
152     unimplemented!()
153 }
154
155 // no warning, two input lifetimes
156 fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
157     unimplemented!()
158 }
159
160 trait WithLifetime<'a> {}
161
162 type WithLifetimeAlias<'a> = WithLifetime<'a>;
163
164 // should not warn because it won't build without the lifetime
165 fn trait_obj_elided<'a>(_arg: &'a WithLifetime) -> &'a str {
166     unimplemented!()
167 }
168
169 // this should warn because there is no lifetime on Drop, so this would be
170 // unambiguous if we elided the lifetime
171 fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str {
172     unimplemented!()
173 }
174
175 type FooAlias<'a> = Foo<'a>;
176
177 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
178     unimplemented!()
179 }
180
181 // no warning, two input lifetimes (named on the reference, anonymous on Foo)
182 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str {
183     unimplemented!()
184 }
185
186 // no warning, two input lifetimes (anonymous on the reference, named on Foo)
187 fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
188     unimplemented!()
189 }
190
191 // no warning, two input lifetimes
192 fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
193     unimplemented!()
194 }
195
196 fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
197     unimplemented!()
198 }
199
200 fn elided_input_named_output<'a>(_arg: &str) -> &'a str {
201     unimplemented!()
202 }
203
204 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
205     unimplemented!()
206 }
207 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) {
208     unimplemented!()
209 }
210
211 // don't warn on these, see #292
212 fn trait_bound_bug<'a, T: WithLifetime<'a>>() {
213     unimplemented!()
214 }
215
216 // #740
217 struct Test {
218     vec: Vec<usize>,
219 }
220
221 impl Test {
222     fn iter<'a>(&'a self) -> Box<Iterator<Item = usize> + 'a> {
223         unimplemented!()
224     }
225 }
226
227 trait LintContext<'a> {}
228
229 fn f<'a, T: LintContext<'a>>(_: &T) {}
230
231 fn test<'a>(x: &'a [u8]) -> u8 {
232     let y: &'a u8 = &x[5];
233     *y
234 }
235
236 // #3284 - Give a hint regarding lifetime in return type
237
238 struct Cow<'a> {
239     x: &'a str,
240 }
241 fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
242     unimplemented!()
243 }
244
245 fn main() {}