Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test that a couple more types of unsafe-ops get a wrapping unsafe blo…
…ck added
  • Loading branch information
Nemo157 committed Jun 13, 2023
commit aca61b2c07f2f7b644ca000385473af9de2d5f77
11 changes: 11 additions & 0 deletions tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
11 changes: 11 additions & 0 deletions tests/ui/unsafe/wrapping-unsafe-block-sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
48 changes: 47 additions & 1 deletion tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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