Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/missing_unsafe.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/missing_unsafe.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/missing_unsafe.rs b/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
index 4bb64747f5..029ed18a4d 100644
--- a/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
+++ b/crates/ide-diagnostics/src/handlers/missing_unsafe.rs
@@ -442,6 +442,49 @@ fn main() {
}
#[test]
+ fn raw_deref_on_union_field() {
+ check_diagnostics(
+ r#"
+fn main() {
+
+ union U {
+ a: u8
+ }
+ let x = U { a: 3 };
+
+ let a = &raw mut x.a;
+
+ union U1 {
+ a: u8
+ }
+ let x = U1 { a: 3 };
+
+ let a = x.a;
+ // ^^^ 💡 error: access to union field is unsafe and requires an unsafe function or block
+
+
+ let b = &raw const x.a;
+
+ let tmp = Vec::from([1, 2, 3]);
+
+ let c = &raw const tmp[x.a];
+ // ^^^ 💡 error: access to union field is unsafe and requires an unsafe function or block
+
+ union URef {
+ p: &'static mut i32,
+ }
+
+ fn deref_union_field(u: URef) {
+ // Not an assignment but an access to the union field!
+ *(u.p) = 13;
+ // ^^^ 💡 error: access to union field is unsafe and requires an unsafe function or block
+ }
+}
+"#,
+ )
+ }
+
+ #[test]
fn unsafe_expr_as_an_argument_of_a_method_call() {
check_fix(
r#"