]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/repl_uninit.rs
Auto merge of #71649 - ecstatic-morse:ci-stage0-doc, r=Mark-Simulacrum
[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::zeroed());
22         let new_v = might_panic(taken_v);
23         std::mem::forget(mem::replace(&mut v, new_v));
24     }
25
26     // this is silly but OK, because usize is a primitive type
27     let mut u: usize = 42;
28     let uref = &mut u;
29     let taken_u = unsafe { mem::replace(uref, mem::zeroed()) };
30     *uref = taken_u + 1;
31
32     // this is still not OK, because uninit
33     let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) };
34     *uref = taken_u + 1;
35 }