]> git.lizzy.rs Git - rust.git/blob - src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
Rollup merge of #103033 - alyssais:pkg-config, r=joshtriplett
[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     //[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
14     //[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
15     *PTR;
16     //~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
17     VOID = ();
18     //~^ ERROR use of mutable static is unsafe and requires unsafe block
19
20     unsafe {}
21     //~^ ERROR unnecessary `unsafe` block
22 }
23
24 // Check that `unsafe_op_in_unsafe_fn` works starting from the `warn` level.
25 #[warn(unsafe_op_in_unsafe_fn)]
26 #[deny(warnings)]
27 unsafe fn warning_level() {
28     unsf();
29     //[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
30     //[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
31     *PTR;
32     //~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
33     VOID = ();
34     //~^ ERROR use of mutable static is unsafe and requires unsafe block
35     unsafe {}
36     //~^ ERROR unnecessary `unsafe` block
37 }
38
39 unsafe fn explicit_block() {
40     // no error
41     unsafe {
42         unsf();
43         *PTR;
44         VOID = ();
45     }
46 }
47
48 unsafe fn two_explicit_blocks() {
49     unsafe { unsafe { unsf() } }
50     //~^ ERROR unnecessary `unsafe` block
51 }
52
53 #[allow(unsafe_op_in_unsafe_fn)]
54 unsafe fn allow_level() {
55     // lint allowed -> no error
56     unsf();
57     *PTR;
58     VOID = ();
59
60     unsafe { unsf() }
61 }
62
63 unsafe fn nested_allow_level() {
64     #[allow(unsafe_op_in_unsafe_fn)]
65     {
66         // lint allowed -> no error
67         unsf();
68         *PTR;
69         VOID = ();
70
71         unsafe { unsf() }
72     }
73 }
74
75 fn main() {
76     unsf();
77     //[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe block
78     //[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
79     #[allow(unsafe_op_in_unsafe_fn)]
80     {
81         unsf();
82         //[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe function or block
83         //[thir]~^^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe function or block
84     }
85 }