granite-rust/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs

77 lines
1.8 KiB
Rust
Raw Normal View History

2020-05-03 23:11:58 +02:00
#![feature(unsafe_block_in_unsafe_fn)]
2020-05-21 23:53:12 +02:00
#![deny(unsafe_op_in_unsafe_fn)]
2020-05-03 23:11:58 +02:00
#![deny(unused_unsafe)]
unsafe fn unsf() {}
2020-05-14 20:37:23 +02:00
const PTR: *const () = std::ptr::null();
static mut VOID: () = ();
2020-05-21 23:53:12 +02:00
unsafe fn deny_level() {
2020-05-03 23:11:58 +02:00
unsf();
2020-05-21 23:53:12 +02:00
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
2020-05-14 20:37:23 +02:00
*PTR;
2020-05-21 23:53:12 +02:00
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
2020-05-14 20:37:23 +02:00
VOID = ();
2020-05-21 23:53:12 +02:00
//~^ ERROR use of mutable static is unsafe and requires unsafe block
2020-05-03 23:11:58 +02:00
}
2020-05-21 23:53:12 +02: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();
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
*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 fn explicit_block() {
2020-05-14 20:37:23 +02:00
// no error
unsafe {
unsf();
*PTR;
VOID = ();
}
2020-05-03 23:11:58 +02:00
}
2020-05-21 23:53:12 +02:00
unsafe fn two_explicit_blocks() {
2020-05-03 23:11:58 +02:00
unsafe { unsafe { unsf() } }
//~^ ERROR unnecessary `unsafe` block
}
#[allow(unsafe_op_in_unsafe_fn)]
2020-05-21 23:53:12 +02:00
unsafe fn allow_level() {
2020-05-14 20:37:23 +02:00
// lint allowed -> no error
unsf();
*PTR;
VOID = ();
2020-05-13 23:43:21 +02:00
unsafe { unsf() }
//~^ ERROR unnecessary `unsafe` block
2020-05-03 23:11:58 +02:00
}
2020-05-21 23:53:12 +02:00
unsafe fn nested_allow_level() {
#[allow(unsafe_op_in_unsafe_fn)]
{
// lint allowed -> no error
unsf();
*PTR;
VOID = ();
unsafe { unsf() }
//~^ ERROR unnecessary `unsafe` block
}
}
2020-05-13 23:43:21 +02:00
fn main() {
2020-05-21 23:53:12 +02:00
unsf();
//~^ ERROR call to unsafe function is unsafe and requires unsafe block
#[allow(unsafe_op_in_unsafe_fn)]
{
unsf();
//~^ ERROR call to unsafe function is unsafe and requires unsafe function or block
}
2020-05-13 23:43:21 +02:00
}