]> git.lizzy.rs Git - rust.git/blob - src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
Rollup merge of #94577 - RalfJung:simd-miri, r=scottmcm
[rust.git] / src / test / ui / unsafe / rfc-2585-unsafe_op_in_unsafe_fn.rs
1 // revisions: mir thir
2 // [thir]compile-flags: -Zthir-unsafeck
3
4 #![deny(unsafe_op_in_unsafe_fn)]
5 #![deny(unused_unsafe)]
6
7 unsafe fn unsf() {}
8 const PTR: *const () = std::ptr::null();
9 static mut VOID: () = ();
10
11 unsafe fn deny_level() {
12     unsf();
13     //~^ ERROR call to unsafe function is unsafe and requires unsafe block
14     *PTR;
15     //~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
16     VOID = ();
17     //~^ ERROR use of mutable static is unsafe and requires unsafe block
18
19     unsafe {}
20     //~^ ERROR unnecessary `unsafe` block
21 }
22
23 // Check that `unsafe_op_in_unsafe_fn` works starting from the `warn` level.
24 #[warn(unsafe_op_in_unsafe_fn)]
25 #[deny(warnings)]
26 unsafe fn warning_level() {
27     unsf();
28     //~^ ERROR call to unsafe function is unsafe and requires unsafe block
29     *PTR;
30     //~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
31     VOID = ();
32     //~^ ERROR use of mutable static is unsafe and requires unsafe block
33     unsafe {}
34     //~^ ERROR unnecessary `unsafe` block
35 }
36
37 unsafe fn explicit_block() {
38     // no error
39     unsafe {
40         unsf();
41         *PTR;
42         VOID = ();
43     }
44 }
45
46 unsafe fn two_explicit_blocks() {
47     unsafe { unsafe { unsf() } }
48     //~^ ERROR unnecessary `unsafe` block
49 }
50
51 #[allow(unsafe_op_in_unsafe_fn)]
52 unsafe fn allow_level() {
53     // lint allowed -> no error
54     unsf();
55     *PTR;
56     VOID = ();
57
58     unsafe { unsf() }
59     //~^ ERROR unnecessary `unsafe` block
60 }
61
62 unsafe fn nested_allow_level() {
63     #[allow(unsafe_op_in_unsafe_fn)]
64     {
65         // lint allowed -> no error
66         unsf();
67         *PTR;
68         VOID = ();
69
70         unsafe { unsf() }
71         //~^ ERROR unnecessary `unsafe` block
72     }
73 }
74
75 fn main() {
76     unsf();
77     //~^ ERROR call to unsafe function is unsafe and requires unsafe block
78     #[allow(unsafe_op_in_unsafe_fn)]
79     {
80         unsf();
81         //~^ ERROR call to unsafe function is unsafe and requires unsafe function or block
82     }
83 }