summaryrefslogtreecommitdiffstats
path: root/tests/ui/unsafe
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs17
-rw-r--r--tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr16
-rw-r--r--tests/ui/unsafe/initializing-ranged-via-ctor.rs11
-rw-r--r--tests/ui/unsafe/initializing-ranged-via-ctor.stderr16
-rw-r--r--tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.rs16
-rw-r--r--tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.stderr21
-rw-r--r--tests/ui/unsafe/ranged-ctor-as-fn-ptr.rs10
-rw-r--r--tests/ui/unsafe/ranged-ctor-as-fn-ptr.stderr15
8 files changed, 122 insertions, 0 deletions
diff --git a/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs
new file mode 100644
index 000000000..a192f3445
--- /dev/null
+++ b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs
@@ -0,0 +1,17 @@
+// edition: 2024
+// compile-flags: -Zunstable-options
+// check-pass
+
+#![crate_type = "lib"]
+
+#![deny(unused_unsafe)]
+
+unsafe fn unsf() {}
+
+unsafe fn foo() {
+ unsf();
+ //~^ WARN call to unsafe function is unsafe and requires unsafe block
+
+ // no unused_unsafe
+ unsafe { unsf(); }
+}
diff --git a/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr
new file mode 100644
index 000000000..fbc621f4d
--- /dev/null
+++ b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr
@@ -0,0 +1,16 @@
+warning: call to unsafe function is unsafe and requires unsafe block (error E0133)
+ --> $DIR/edition-2024-unsafe_op_in_unsafe_fn.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/edition-2024-unsafe_op_in_unsafe_fn.rs:11:1
+ |
+LL | unsafe fn foo() {
+ | ^^^^^^^^^^^^^^^
+ = note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default
+
+warning: 1 warning emitted
+
diff --git a/tests/ui/unsafe/initializing-ranged-via-ctor.rs b/tests/ui/unsafe/initializing-ranged-via-ctor.rs
new file mode 100644
index 000000000..ca44fa7e4
--- /dev/null
+++ b/tests/ui/unsafe/initializing-ranged-via-ctor.rs
@@ -0,0 +1,11 @@
+#![feature(rustc_attrs)]
+#![allow(internal_features)]
+
+#[derive(Debug)]
+#[rustc_layout_scalar_valid_range_start(2)]
+struct NonZeroAndOneU8(u8);
+
+fn main() {
+ println!("{:?}", Some(1).map(NonZeroAndOneU8).unwrap());
+ //~^ ERROR found `unsafe fn(u8) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
+}
diff --git a/tests/ui/unsafe/initializing-ranged-via-ctor.stderr b/tests/ui/unsafe/initializing-ranged-via-ctor.stderr
new file mode 100644
index 000000000..d34554c66
--- /dev/null
+++ b/tests/ui/unsafe/initializing-ranged-via-ctor.stderr
@@ -0,0 +1,16 @@
+error[E0277]: expected a `FnOnce<({integer},)>` closure, found `unsafe fn(u8) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
+ --> $DIR/initializing-ranged-via-ctor.rs:9:34
+ |
+LL | println!("{:?}", Some(1).map(NonZeroAndOneU8).unwrap());
+ | --- ^^^^^^^^^^^^^^^ call the function in a closure: `|| unsafe { /* code */ }`
+ | |
+ | required by a bound introduced by this call
+ |
+ = help: the trait `FnOnce<({integer},)>` is not implemented for fn item `unsafe fn(u8) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
+ = note: unsafe function cannot be called generically without an unsafe block
+note: required by a bound in `Option::<T>::map`
+ --> $SRC_DIR/core/src/option.rs:LL:COL
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.rs b/tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.rs
new file mode 100644
index 000000000..68559338d
--- /dev/null
+++ b/tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.rs
@@ -0,0 +1,16 @@
+// Regression test for #115348.
+
+unsafe fn uwu() {}
+
+// Tests that the false-positive warning "unnecessary `unsafe` block"
+// should not be reported, when the error "non-exhaustive patterns"
+// appears.
+
+fn foo(x: Option<u32>) {
+ match x {
+ //~^ ERROR non-exhaustive patterns: `None` not covered
+ Some(_) => unsafe { uwu() },
+ }
+}
+
+fn main() {}
diff --git a/tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.stderr b/tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.stderr
new file mode 100644
index 000000000..7384899b9
--- /dev/null
+++ b/tests/ui/unsafe/issue-115348-false-positive-warning-of-unnecessary-unsafe.stderr
@@ -0,0 +1,21 @@
+error[E0004]: non-exhaustive patterns: `None` not covered
+ --> $DIR/issue-115348-false-positive-warning-of-unnecessary-unsafe.rs:10:11
+ |
+LL | match x {
+ | ^ pattern `None` not covered
+ |
+note: `Option<u32>` defined here
+ --> $SRC_DIR/core/src/option.rs:LL:COL
+ ::: $SRC_DIR/core/src/option.rs:LL:COL
+ |
+ = note: not covered
+ = note: the matched value is of type `Option<u32>`
+help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
+ |
+LL ~ Some(_) => unsafe { uwu() },
+LL ~ None => todo!(),
+ |
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0004`.
diff --git a/tests/ui/unsafe/ranged-ctor-as-fn-ptr.rs b/tests/ui/unsafe/ranged-ctor-as-fn-ptr.rs
new file mode 100644
index 000000000..a91e57951
--- /dev/null
+++ b/tests/ui/unsafe/ranged-ctor-as-fn-ptr.rs
@@ -0,0 +1,10 @@
+#![feature(rustc_attrs)]
+
+#[derive(Debug)]
+#[rustc_layout_scalar_valid_range_start(2)]
+struct NonZeroAndOneU8(u8);
+
+fn main() {
+ let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
+ //~^ ERROR mismatched types
+}
diff --git a/tests/ui/unsafe/ranged-ctor-as-fn-ptr.stderr b/tests/ui/unsafe/ranged-ctor-as-fn-ptr.stderr
new file mode 100644
index 000000000..660c40704
--- /dev/null
+++ b/tests/ui/unsafe/ranged-ctor-as-fn-ptr.stderr
@@ -0,0 +1,15 @@
+error[E0308]: mismatched types
+ --> $DIR/ranged-ctor-as-fn-ptr.rs:8:40
+ |
+LL | let x: fn(u8) -> NonZeroAndOneU8 = NonZeroAndOneU8;
+ | ------------------------- ^^^^^^^^^^^^^^^ expected normal fn, found unsafe fn
+ | |
+ | expected due to this
+ |
+ = note: expected fn pointer `fn(_) -> NonZeroAndOneU8`
+ found struct constructor `unsafe fn(_) -> NonZeroAndOneU8 {NonZeroAndOneU8}`
+ = note: unsafe functions cannot be coerced into safe function pointers
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.