rust/tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.2 KiB
Rust
Raw Normal View History

// revisions: mir thir
// [thir]compile-flags: -Zthir-unsafeck
2020-05-21 16:53:12 -05:00
#![deny(unsafe_op_in_unsafe_fn)]
2020-05-03 16:11:58 -05:00
#![deny(unused_unsafe)]
unsafe fn unsf() {}
2020-05-14 13:37:23 -05:00
const PTR: *const () = std::ptr::null();
static mut VOID: () = ();
2020-05-21 16:53:12 -05:00
unsafe fn deny_level() {
2020-05-03 16:11:58 -05:00
unsf();
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
2020-05-14 13:37:23 -05:00
*PTR;
2020-05-21 16:53:12 -05:00
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
2020-05-14 13:37:23 -05:00
VOID = ();
2020-05-21 16:53:12 -05:00
//~^ ERROR use of mutable static is unsafe and requires unsafe block
unsafe {}
//~^ ERROR unnecessary `unsafe` block
2020-05-03 16:11:58 -05:00
}
2020-05-21 16:53:12 -05:00
// Check that `unsafe_op_in_unsafe_fn` works starting from the `warn` level.
#[warn(unsafe_op_in_unsafe_fn)]
#[deny(warnings)]
unsafe fn warning_level() {
unsf();
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
2020-05-21 16:53:12 -05:00
*PTR;
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
VOID = ();
//~^ ERROR use of mutable static is unsafe and requires unsafe block
unsafe {}
//~^ ERROR unnecessary `unsafe` block
2020-05-21 16:53:12 -05:00
}
unsafe fn explicit_block() {
2020-05-14 13:37:23 -05:00
// no error
unsafe {
unsf();
*PTR;
VOID = ();
}
2020-05-03 16:11:58 -05:00
}
2020-05-21 16:53:12 -05:00
unsafe fn two_explicit_blocks() {
2020-05-03 16:11:58 -05:00
unsafe { unsafe { unsf() } }
//~^ ERROR unnecessary `unsafe` block
}
#[allow(unsafe_op_in_unsafe_fn)]
2020-05-21 16:53:12 -05:00
unsafe fn allow_level() {
2020-05-14 13:37:23 -05:00
// lint allowed -> no error
unsf();
*PTR;
VOID = ();
2020-05-13 16:43:21 -05:00
unsafe { unsf() }
2020-05-03 16:11:58 -05:00
}
2020-05-21 16:53:12 -05:00
unsafe fn nested_allow_level() {
#[allow(unsafe_op_in_unsafe_fn)]
{
// lint allowed -> no error
unsf();
*PTR;
VOID = ();
unsafe { unsf() }
}
}
2020-05-13 16:43:21 -05:00
fn main() {
2020-05-21 16:53:12 -05:00
unsf();
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
2020-05-21 16:53:12 -05:00
#[allow(unsafe_op_in_unsafe_fn)]
{
unsf();
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe function or block
//[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe function or block
2020-05-21 16:53:12 -05:00
}
2020-05-13 16:43:21 -05:00
}