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