2019-08-31 18:25:28 +00:00
|
|
|
#![feature(stmt_expr_attributes)]
|
2022-08-29 20:28:35 +00:00
|
|
|
#![allow(clippy::let_unit_value, invalid_value)]
|
2019-08-31 18:25:28 +00:00
|
|
|
|
2023-04-11 13:31:08 +00:00
|
|
|
use std::mem::MaybeUninit;
|
2019-08-31 18:25:28 +00:00
|
|
|
|
2023-03-24 13:04:35 +00:00
|
|
|
union MyOwnMaybeUninit {
|
|
|
|
value: u8,
|
|
|
|
uninit: (),
|
|
|
|
}
|
|
|
|
|
2019-08-31 18:25:28 +00:00
|
|
|
fn main() {
|
|
|
|
let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this call for this type may be undefined behavior
|
|
|
|
//~| NOTE: `#[deny(clippy::uninit_assumed_init)]` on by default
|
2019-08-31 18:25:28 +00:00
|
|
|
|
2023-03-24 13:04:35 +00:00
|
|
|
// This is OK, because ZSTs do not contain data.
|
2019-08-31 18:25:28 +00:00
|
|
|
let _: () = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
|
|
|
// This is OK, because `MaybeUninit` allows uninitialized data.
|
|
|
|
let _: MaybeUninit<usize> = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
2023-05-05 15:45:49 +00:00
|
|
|
// This is OK, because all constituent types are uninit-compatible.
|
2019-08-31 18:25:28 +00:00
|
|
|
let _: (MaybeUninit<usize>, MaybeUninit<bool>) = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
2023-05-05 15:45:49 +00:00
|
|
|
// This is OK, because all constituent types are uninit-compatible.
|
2019-08-31 18:25:28 +00:00
|
|
|
let _: (MaybeUninit<usize>, [MaybeUninit<bool>; 2]) = unsafe { MaybeUninit::uninit().assume_init() };
|
2021-04-22 09:31:13 +00:00
|
|
|
|
2023-03-24 13:04:35 +00:00
|
|
|
// This is OK, because our own MaybeUninit is just as fine as the one from core.
|
|
|
|
let _: MyOwnMaybeUninit = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
|
|
|
// This is OK, because empty arrays don't contain data.
|
|
|
|
let _: [u8; 0] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
// Was a false negative.
|
2023-04-11 13:31:08 +00:00
|
|
|
let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this call for this type may be undefined behavior
|
2023-03-24 13:04:35 +00:00
|
|
|
|
|
|
|
polymorphic::<()>();
|
2023-04-11 13:31:08 +00:00
|
|
|
polymorphic_maybe_uninit_array::<10>();
|
|
|
|
polymorphic_maybe_uninit::<u8>();
|
2023-03-24 13:04:35 +00:00
|
|
|
|
|
|
|
fn polymorphic<T>() {
|
|
|
|
// We are conservative around polymorphic types.
|
2023-04-11 13:31:08 +00:00
|
|
|
let _: T = unsafe { MaybeUninit::uninit().assume_init() };
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: this call for this type may be undefined behavior
|
2023-04-11 13:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn polymorphic_maybe_uninit_array<const N: usize>() {
|
|
|
|
// While the type is polymorphic, MaybeUninit<u8> is not.
|
|
|
|
let _: [MaybeUninit<u8>; N] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
}
|
|
|
|
|
|
|
|
fn polymorphic_maybe_uninit<T>() {
|
|
|
|
// The entire type is polymorphic, but it's wrapped in a MaybeUninit.
|
|
|
|
let _: MaybeUninit<T> = unsafe { MaybeUninit::uninit().assume_init() };
|
2023-03-24 13:04:35 +00:00
|
|
|
}
|
2019-08-31 18:25:28 +00:00
|
|
|
}
|