]> git.lizzy.rs Git - rust.git/blob - tests/ui/lifetimes.rs
Adapt the *.stderr files of the ui-tests to the tool_lints
[rust.git] / tests / ui / lifetimes.rs
1 #![feature(tool_lints)]
2
3
4 #![warn(clippy::needless_lifetimes, clippy::extra_unused_lifetimes)]
5 #![allow(dead_code, clippy::needless_pass_by_value, clippy::trivially_copy_pass_by_ref)]
6
7 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) { }
8
9 fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) { }
10
11 fn same_lifetime_on_input<'a>(_x: &'a u8, _y: &'a u8) { } // no error, same lifetime on two params
12
13 fn only_static_on_input(_x: &u8, _y: &u8, _z: &'static u8) { } // no error, static involved
14
15 fn mut_and_static_input(_x: &mut u8, _y: &'static str) { }
16
17 fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 { x }
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
31 // where clause, but without lifetimes
32 fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> where T: Copy { Ok(x) }
33
34 type Ref<'r> = &'r u8;
35
36 fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) { } // no error, same lifetime on two params
37
38 fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) { }
39
40 fn lifetime_param_3<'a, 'b: 'a>(_x: Ref<'a>, _y: &'b u8) { } // no error, bounded lifetime
41
42 fn lifetime_param_4<'a, 'b>(_x: Ref<'a>, _y: &'b u8) where 'b: 'a { } // no error, bounded lifetime
43
44 struct Lt<'a, I: 'static> {
45     x: &'a I
46 }
47
48 fn fn_bound<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
49     where F: Fn(Lt<'a, I>) -> Lt<'a, I>  // no error, fn bound references 'a
50 { unreachable!() }
51
52 fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
53     where for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>
54 { unreachable!() }
55
56 fn fn_bound_3<'a, F: FnOnce(&'a i32)>(x: &'a i32, f: F) { // no error, see below
57     f(x);
58 }
59
60 fn fn_bound_3_cannot_elide() {
61     let x = 42;
62     let p = &x;
63     let mut q = &x;
64     fn_bound_3(p, |y| q = y); // this will fail if we elides lifetimes of `fn_bound_3`
65 }
66
67 // no error, multiple input refs
68 fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(cond: bool, x: &'a (), f: F) -> &'a () {
69     if cond { x } else { f() }
70 }
71
72 struct X {
73     x: u8,
74 }
75
76 impl X {
77     fn self_and_out<'s>(&'s self) -> &'s u8 { &self.x }
78
79     fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 { &self.x } // no error, multiple input refs
80
81     fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) { }
82
83     fn self_and_same_in<'s>(&'s self, _x: &'s u8) { } // no error, same lifetimes on two params
84 }
85
86 struct Foo<'a>(&'a u8);
87
88 impl<'a> Foo<'a> {
89     fn self_shared_lifetime(&self, _: &'a u8) {} // no error, lifetime 'a not defined in method
90     fn self_bound_lifetime<'b: 'a>(&self, _: &'b u8) {} // no error, bounds exist
91 }
92
93 fn already_elided<'a>(_: &u8, _: &'a u8) -> &'a u8 {
94     unimplemented!()
95 }
96
97 fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { unimplemented!() }
98
99 // no warning, two input lifetimes (named on the reference, anonymous on Foo)
100 fn struct_with_lt2<'a>(_foo: &'a Foo) -> &'a str { unimplemented!() }
101
102 // no warning, two input lifetimes (anonymous on the reference, named on Foo)
103 fn struct_with_lt3<'a>(_foo: &Foo<'a> ) -> &'a str { unimplemented!() }
104
105 // no warning, two input lifetimes
106 fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b> ) -> &'a str { unimplemented!() }
107
108 trait WithLifetime<'a> {}
109
110 type WithLifetimeAlias<'a> = WithLifetime<'a>;
111
112 // should not warn because it won't build without the lifetime
113 fn trait_obj_elided<'a>(_arg: &'a WithLifetime) -> &'a str { unimplemented!() }
114
115 // this should warn because there is no lifetime on Drop, so this would be
116 // unambiguous if we elided the lifetime
117 fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str { unimplemented!() }
118
119 type FooAlias<'a> = Foo<'a>;
120
121 fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { unimplemented!() }
122
123 // no warning, two input lifetimes (named on the reference, anonymous on Foo)
124 fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { unimplemented!() }
125
126 // no warning, two input lifetimes (anonymous on the reference, named on Foo)
127 fn alias_with_lt3<'a>(_foo: &FooAlias<'a> ) -> &'a str { unimplemented!() }
128
129 // no warning, two input lifetimes
130 fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b> ) -> &'a str { unimplemented!() }
131
132 fn named_input_elided_output<'a>(_arg: &'a str) -> &str { unimplemented!() }
133
134 fn elided_input_named_output<'a>(_arg: &str) -> &'a str { unimplemented!() }
135
136 fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) { unimplemented!() }
137 fn trait_bound<'a, T: WithLifetime<'a>>(_: &'a u8, _: T) { unimplemented!() }
138
139 // don't warn on these, see #292
140 fn trait_bound_bug<'a, T: WithLifetime<'a>>() { unimplemented!() }
141
142 // #740
143 struct Test {
144     vec: Vec<usize>,
145 }
146
147 impl Test {
148     fn iter<'a>(&'a self) -> Box<Iterator<Item = usize> + 'a> {
149         unimplemented!()
150     }
151 }
152
153
154 trait LintContext<'a> {}
155
156 fn f<'a, T: LintContext<'a>>(_: &T) {}
157
158 fn test<'a>(x: &'a [u8]) -> u8 {
159     let y: &'a u8 = &x[5];
160     *y
161 }
162
163 fn main() {
164 }