summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/repl_uninit.rs
blob: 01bdf79e6422532b7f2f18c0fd9f12330b8d071b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#![allow(deprecated, invalid_value, clippy::uninit_assumed_init)]
#![warn(clippy::mem_replace_with_uninit)]
//@no-rustfix
use std::mem;

fn might_panic<X>(x: X) -> X {
    // in practice this would be a possibly-panicky operation
    x
}

fn main() {
    let mut v = vec![0i32; 4];
    // the following is UB if `might_panic` panics
    unsafe {
        let taken_v = mem::replace(&mut v, mem::uninitialized());
        //~^ ERROR: replacing with `mem::uninitialized()`
        //~| NOTE: `-D clippy::mem-replace-with-uninit` implied by `-D warnings`
        let new_v = might_panic(taken_v);
        std::mem::forget(mem::replace(&mut v, new_v));
    }

    unsafe {
        let taken_v = mem::replace(&mut v, mem::MaybeUninit::uninit().assume_init());
        //~^ ERROR: replacing with `mem::MaybeUninit::uninit().assume_init()`
        let new_v = might_panic(taken_v);
        std::mem::forget(mem::replace(&mut v, new_v));
    }

    unsafe {
        let taken_v = mem::replace(&mut v, mem::zeroed());
        //~^ ERROR: replacing with `mem::zeroed()`
        let new_v = might_panic(taken_v);
        std::mem::forget(mem::replace(&mut v, new_v));
    }

    // this is silly but OK, because usize is a primitive type
    let mut u: usize = 42;
    let uref = &mut u;
    let taken_u = unsafe { mem::replace(uref, mem::zeroed()) };
    *uref = taken_u + 1;

    // this is still not OK, because uninit
    let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) };
    //~^ ERROR: replacing with `mem::uninitialized()`
    *uref = taken_u + 1;
}