]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/repl_uninit.rs
Rollup merge of #73681 - jackh726:chalk-0.14, r=nikomatsakis
[rust.git] / src / tools / clippy / tests / ui / repl_uninit.rs
1 #![allow(deprecated, invalid_value)]
2 #![warn(clippy::all)]
3
4 use std::mem;
5
6 fn might_panic<X>(x: X) -> X {
7     // in practice this would be a possibly-panicky operation
8     x
9 }
10
11 fn main() {
12     let mut v = vec![0i32; 4];
13     // the following is UB if `might_panic` panics
14     unsafe {
15         let taken_v = mem::replace(&mut v, mem::uninitialized());
16         let new_v = might_panic(taken_v);
17         std::mem::forget(mem::replace(&mut v, new_v));
18     }
19
20     unsafe {
21         let taken_v = mem::replace(&mut v, mem::MaybeUninit::uninit().assume_init());
22         let new_v = might_panic(taken_v);
23         std::mem::forget(mem::replace(&mut v, new_v));
24     }
25
26     unsafe {
27         let taken_v = mem::replace(&mut v, mem::zeroed());
28         let new_v = might_panic(taken_v);
29         std::mem::forget(mem::replace(&mut v, new_v));
30     }
31
32     // this is silly but OK, because usize is a primitive type
33     let mut u: usize = 42;
34     let uref = &mut u;
35     let taken_u = unsafe { mem::replace(uref, mem::zeroed()) };
36     *uref = taken_u + 1;
37
38     // this is still not OK, because uninit
39     let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) };
40     *uref = taken_u + 1;
41 }