]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/lifetimes.rs
Merge pull request #158 from birkenfeld/iter_methods
[rust.git] / tests / compile-fail / lifetimes.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #![deny(needless_lifetimes)]
5
6 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) { }
7 //~^ERROR explicit lifetimes given
8
9 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) { }
10 //~^ERROR explicit lifetimes given
11
12 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) { } // no error, same lifetime on two params
13
14 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) { } // no error, static involved
15
16 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 { x }
17 //~^ERROR explicit lifetimes given
18
19 fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 { x } // no error, multiple input refs
20
21 fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 { x } // no error, multiple input refs
22
23 fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 { x } // no error, static involved
24
25 fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> { Ok(x) } // no error
26
27 fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 { x.unwrap() } // no error, two input refs
28
29 fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> { Ok(x) }
30 //~^ERROR explicit lifetimes given
31
32 type Ref<'r> = &'r u8;
33
34 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) { }
35
36 fn lifetime_param_2<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) { }
37 //~^ERROR explicit lifetimes given
38
39 struct X {
40     x: u8,
41 }
42
43 impl X {
44     fn self_and_out<'s>(&'s self) -> &'s u8 { &self.x }
45     //~^ERROR explicit lifetimes given
46
47     fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 { &self.x } // no error, multiple input refs
48
49     fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) { }
50     //~^ERROR explicit lifetimes given
51
52     fn self_and_same_in<'s>(&'s self, _x: &'s u8) { } // no error, same lifetimes on two params
53 }
54
55 static STATIC: u8 = 1;
56
57 fn main() {
58     distinct_lifetimes(&1, &2, 3);
59     distinct_and_static(&1, &2, &STATIC);
60     same_lifetime_on_input(&1, &2);
61     only_static_on_input(&1, &2, &STATIC);
62     in_and_out(&1, 2);
63     multiple_in_and_out_1(&1, &2);
64     multiple_in_and_out_2(&1, &2);
65     in_static_and_out(&1, &STATIC);
66     let _ = deep_reference_1(&1, &2);
67     let _ = deep_reference_2(Ok(&1));
68     let _ = deep_reference_3(&1, 2);
69     lifetime_param_1(&1, &2);
70     lifetime_param_2(&1, &2);
71
72     let foo = X { x: 1 };
73     foo.self_and_out();
74     foo.self_and_in_out(&1);
75     foo.distinct_self_and_in(&1);
76     foo.self_and_same_in(&1);
77 }