Skip to content

Commit aca61b2

Browse files
committed
Test that a couple more types of unsafe-ops get a wrapping unsafe block added
1 parent 975152c commit aca61b2

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,15 @@ pub unsafe fn foo() { unsafe {
99
unsf(); //~ ERROR call to unsafe function is unsafe
1010
}}
1111

12+
pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
13+
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
14+
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
15+
}}
16+
17+
static mut BAZ: i32 = 0;
18+
pub unsafe fn baz() -> i32 { unsafe {
19+
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
20+
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
21+
}}
22+
1223
fn main() {}

tests/ui/unsafe/wrapping-unsafe-block-sugg.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,15 @@ pub unsafe fn foo() {
99
unsf(); //~ ERROR call to unsafe function is unsafe
1010
}
1111

12+
pub unsafe fn bar(x: *const i32) -> i32 {
13+
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
14+
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
15+
}
16+
17+
static mut BAZ: i32 = 0;
18+
pub unsafe fn baz() -> i32 {
19+
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
20+
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
21+
}
22+
1223
fn main() {}

tests/ui/unsafe/wrapping-unsafe-block-sugg.stderr

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,51 @@ LL | unsf();
2626
|
2727
= note: consult the function's documentation for information on how to avoid undefined behavior
2828

29-
error: aborting due to 2 previous errors
29+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
30+
--> $DIR/wrapping-unsafe-block-sugg.rs:13:13
31+
|
32+
LL | let y = *x;
33+
| ^^ dereference of raw pointer
34+
|
35+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
36+
help: consider wrapping the function body in an unsafe block
37+
|
38+
LL ~ pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
39+
LL | let y = *x;
40+
LL | y + *x
41+
LL ~ }}
42+
|
43+
44+
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
45+
--> $DIR/wrapping-unsafe-block-sugg.rs:14:9
46+
|
47+
LL | y + *x
48+
| ^^ dereference of raw pointer
49+
|
50+
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
51+
52+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
53+
--> $DIR/wrapping-unsafe-block-sugg.rs:19:13
54+
|
55+
LL | let y = BAZ;
56+
| ^^^ use of mutable static
57+
|
58+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
59+
help: consider wrapping the function body in an unsafe block
60+
|
61+
LL ~ pub unsafe fn baz() -> i32 { unsafe {
62+
LL | let y = BAZ;
63+
LL | y + BAZ
64+
LL ~ }}
65+
|
66+
67+
error: use of mutable static is unsafe and requires unsafe block (error E0133)
68+
--> $DIR/wrapping-unsafe-block-sugg.rs:20:9
69+
|
70+
LL | y + BAZ
71+
| ^^^ use of mutable static
72+
|
73+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
74+
75+
error: aborting due to 6 previous errors
3076

0 commit comments

Comments
 (0)