From aca61b2c07f2f7b644ca000385473af9de2d5f77 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 27 May 2023 14:37:18 +0200 Subject: [PATCH] Test that a couple more types of unsafe-ops get a wrapping unsafe block added --- .../unsafe/wrapping-unsafe-block-sugg.fixed | 11 +++++ tests/ui/unsafe/wrapping-unsafe-block-sugg.rs | 11 +++++ .../unsafe/wrapping-unsafe-block-sugg.stderr | 48 ++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed b/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed index 81b7d68bc4a..c36aabb7c92 100644 --- a/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed +++ b/tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed @@ -9,4 +9,15 @@ pub unsafe fn foo() { unsafe { unsf(); //~ ERROR call to unsafe function is unsafe }} +pub unsafe fn bar(x: *const i32) -> i32 { unsafe { + let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block + y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block +}} + +static mut BAZ: i32 = 0; +pub unsafe fn baz() -> i32 { unsafe { + let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block + y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block +}} + fn main() {} diff --git a/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs b/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs index ef6936d91d1..95e22d1bc4d 100644 --- a/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs +++ b/tests/ui/unsafe/wrapping-unsafe-block-sugg.rs @@ -9,4 +9,15 @@ pub unsafe fn foo() { unsf(); //~ ERROR call to unsafe function is unsafe } +pub unsafe fn bar(x: *const i32) -> i32 { + let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block + y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block +} + +static mut BAZ: i32 = 0; +pub unsafe fn baz() -> i32 { + let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block + y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block +} + fn main() {} diff --git a/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr b/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr index 7283ee08bdf..b05d87069ab 100644 --- a/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr +++ b/tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr @@ -26,5 +26,51 @@ LL | unsf(); | = note: consult the function's documentation for information on how to avoid undefined behavior -error: aborting due to 2 previous errors +error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) + --> $DIR/wrapping-unsafe-block-sugg.rs:13: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 +help: consider wrapping the function body in an unsafe block + | +LL ~ pub unsafe fn bar(x: *const i32) -> i32 { unsafe { +LL | let y = *x; +LL | y + *x +LL ~ }} + | + +error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) + --> $DIR/wrapping-unsafe-block-sugg.rs:14: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:19: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 +help: consider wrapping the function body in an unsafe block + | +LL ~ pub unsafe fn baz() -> i32 { unsafe { +LL | let y = BAZ; +LL | y + BAZ +LL ~ }} + | + +error: use of mutable static is unsafe and requires unsafe block (error E0133) + --> $DIR/wrapping-unsafe-block-sugg.rs:20: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: aborting due to 6 previous errors