summaryrefslogtreecommitdiffstats
path: root/tests/ui/unsafe
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/unsafe')
-rw-r--r--tests/ui/unsafe/auxiliary/external_unsafe_macro.rs4
-rw-r--r--tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr10
-rw-r--r--tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed66
-rw-r--r--tests/ui/unsafe/wrapping-unsafe-block-sugg.rs66
-rw-r--r--tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr99
5 files changed, 245 insertions, 0 deletions
diff --git a/tests/ui/unsafe/auxiliary/external_unsafe_macro.rs b/tests/ui/unsafe/auxiliary/external_unsafe_macro.rs
new file mode 100644
index 000000000..b3837338b
--- /dev/null
+++ b/tests/ui/unsafe/auxiliary/external_unsafe_macro.rs
@@ -0,0 +1,4 @@
+pub unsafe fn unsf() {}
+
+#[macro_export]
+macro_rules! unsafe_macro { () => ($crate::unsf()) }
diff --git a/tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr b/tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr
index 6f005fe89..0c0826c1c 100644
--- a/tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr
+++ b/tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr
@@ -5,6 +5,11 @@ LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
+note: an unsafe function restricts its caller, but its body is safe by default
+ --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:11:1
+ |
+LL | unsafe fn deny_level() {
+ | ^^^^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:4:9
|
@@ -46,6 +51,11 @@ LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
+note: an unsafe function restricts its caller, but its body is safe by default
+ --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:1
+ |
+LL | unsafe fn warning_level() {
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:8
|
diff --git a/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed b/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed
new file mode 100644
index 000000000..db1c10221
--- /dev/null
+++ b/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed
@@ -0,0 +1,66 @@
+// run-rustfix
+// aux-build:external_unsafe_macro.rs
+
+#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
+
+extern crate external_unsafe_macro;
+
+unsafe fn unsf() {}
+
+pub unsafe fn foo() { unsafe {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ unsf(); //~ ERROR call to unsafe function is unsafe
+ //~^ NOTE
+ //~| NOTE
+ unsf(); //~ ERROR call to unsafe function is unsafe
+ //~^ NOTE
+ //~| NOTE
+}}
+
+pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+ y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+}}
+
+static mut BAZ: i32 = 0;
+pub unsafe fn baz() -> i32 { unsafe {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+ y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+}}
+
+macro_rules! unsafe_macro { () => (unsf()) }
+//~^ ERROR call to unsafe function is unsafe
+//~| NOTE
+//~| NOTE
+//~| ERROR call to unsafe function is unsafe
+//~| NOTE
+//~| NOTE
+
+pub unsafe fn unsafe_in_macro() { unsafe {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ unsafe_macro!();
+ //~^ NOTE
+ //~| NOTE
+ unsafe_macro!();
+ //~^ NOTE
+ //~| NOTE
+}}
+
+pub unsafe fn unsafe_in_external_macro() {
+ // FIXME: https://github.com/rust-lang/rust/issues/112504
+ // FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ external_unsafe_macro::unsafe_macro!();
+ external_unsafe_macro::unsafe_macro!();
+}
+
+fn main() {}
diff --git a/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs b/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs
new file mode 100644
index 000000000..9c6be4589
--- /dev/null
+++ b/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs
@@ -0,0 +1,66 @@
+// run-rustfix
+// aux-build:external_unsafe_macro.rs
+
+#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE
+
+extern crate external_unsafe_macro;
+
+unsafe fn unsf() {}
+
+pub unsafe fn foo() {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ unsf(); //~ ERROR call to unsafe function is unsafe
+ //~^ NOTE
+ //~| NOTE
+ unsf(); //~ ERROR call to unsafe function is unsafe
+ //~^ NOTE
+ //~| NOTE
+}
+
+pub unsafe fn bar(x: *const i32) -> i32 {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+ y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+}
+
+static mut BAZ: i32 = 0;
+pub unsafe fn baz() -> i32 {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+ y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
+ //~^ NOTE
+ //~| NOTE
+}
+
+macro_rules! unsafe_macro { () => (unsf()) }
+//~^ ERROR call to unsafe function is unsafe
+//~| NOTE
+//~| NOTE
+//~| ERROR call to unsafe function is unsafe
+//~| NOTE
+//~| NOTE
+
+pub unsafe fn unsafe_in_macro() {
+ //~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ unsafe_macro!();
+ //~^ NOTE
+ //~| NOTE
+ unsafe_macro!();
+ //~^ NOTE
+ //~| NOTE
+}
+
+pub unsafe fn unsafe_in_external_macro() {
+ // FIXME: https://github.com/rust-lang/rust/issues/112504
+ // FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default
+ external_unsafe_macro::unsafe_macro!();
+ external_unsafe_macro::unsafe_macro!();
+}
+
+fn main() {}
diff --git a/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr b/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr
new file mode 100644
index 000000000..43f619c27
--- /dev/null
+++ b/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr
@@ -0,0 +1,99 @@
+error: call to unsafe function is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:12:5
+ |
+LL | unsf();
+ | ^^^^^^ call to unsafe function
+ |
+ = note: consult the function's documentation for information on how to avoid undefined behavior
+note: an unsafe function restricts its caller, but its body is safe by default
+ --> $DIR/wrapping-unsafe-block-sugg.rs:10:1
+ |
+LL | pub unsafe fn foo() {
+ | ^^^^^^^^^^^^^^^^^^^
+note: the lint level is defined here
+ --> $DIR/wrapping-unsafe-block-sugg.rs:4:9
+ |
+LL | #![deny(unsafe_op_in_unsafe_fn)]
+ | ^^^^^^^^^^^^^^^^^^^^^^
+
+error: call to unsafe function is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:15:5
+ |
+LL | unsf();
+ | ^^^^^^ call to unsafe function
+ |
+ = note: consult the function's documentation for information on how to avoid undefined behavior
+
+error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:22:13
+ |
+LL | let y = *x;
+ | ^^ dereference of raw pointer
+ |
+ = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
+note: an unsafe function restricts its caller, but its body is safe by default
+ --> $DIR/wrapping-unsafe-block-sugg.rs:20:1
+ |
+LL | pub unsafe fn bar(x: *const i32) -> i32 {
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:25:9
+ |
+LL | y + *x
+ | ^^ dereference of raw pointer
+ |
+ = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
+
+error: use of mutable static is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:33:13
+ |
+LL | let y = BAZ;
+ | ^^^ use of mutable static
+ |
+ = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
+note: an unsafe function restricts its caller, but its body is safe by default
+ --> $DIR/wrapping-unsafe-block-sugg.rs:31:1
+ |
+LL | pub unsafe fn baz() -> i32 {
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: use of mutable static is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:36:9
+ |
+LL | y + BAZ
+ | ^^^ use of mutable static
+ |
+ = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
+
+error: call to unsafe function is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:41:36
+ |
+LL | macro_rules! unsafe_macro { () => (unsf()) }
+ | ^^^^^^ call to unsafe function
+...
+LL | unsafe_macro!();
+ | --------------- in this macro invocation
+ |
+ = note: consult the function's documentation for information on how to avoid undefined behavior
+note: an unsafe function restricts its caller, but its body is safe by default
+ --> $DIR/wrapping-unsafe-block-sugg.rs:49:1
+ |
+LL | pub unsafe fn unsafe_in_macro() {
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ = note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: call to unsafe function is unsafe and requires unsafe block (error E0133)
+ --> $DIR/wrapping-unsafe-block-sugg.rs:41:36
+ |
+LL | macro_rules! unsafe_macro { () => (unsf()) }
+ | ^^^^^^ call to unsafe function
+...
+LL | unsafe_macro!();
+ | --------------- in this macro invocation
+ |
+ = note: consult the function's documentation for information on how to avoid undefined behavior
+ = note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 8 previous errors
+