summaryrefslogtreecommitdiffstats
path: root/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs')
-rw-r--r--src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
new file mode 100644
index 000000000..30b072340
--- /dev/null
+++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs
@@ -0,0 +1,87 @@
+// revisions: mir thir
+// [thir]compile-flags: -Zthir-unsafeck
+
+#![deny(unsafe_op_in_unsafe_fn)]
+#![deny(unused_unsafe)]
+
+unsafe fn unsf() {}
+const PTR: *const () = std::ptr::null();
+static mut VOID: () = ();
+
+unsafe fn deny_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
+ *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
+}
+
+// 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
+ *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
+}
+
+unsafe fn explicit_block() {
+ // no error
+ unsafe {
+ unsf();
+ *PTR;
+ VOID = ();
+ }
+}
+
+unsafe fn two_explicit_blocks() {
+ unsafe { unsafe { unsf() } }
+ //~^ ERROR unnecessary `unsafe` block
+}
+
+#[allow(unsafe_op_in_unsafe_fn)]
+unsafe fn allow_level() {
+ // lint allowed -> no error
+ unsf();
+ *PTR;
+ VOID = ();
+
+ unsafe { unsf() }
+ //~^ ERROR unnecessary `unsafe` block
+}
+
+unsafe fn nested_allow_level() {
+ #[allow(unsafe_op_in_unsafe_fn)]
+ {
+ // lint allowed -> no error
+ unsf();
+ *PTR;
+ VOID = ();
+
+ unsafe { unsf() }
+ //~^ ERROR unnecessary `unsafe` block
+ }
+}
+
+fn main() {
+ 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
+ #[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
+ }
+}