about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2023-08-22 15:41:07 +0200
committerUrgau <urgau@numericable.fr>2023-08-22 15:47:29 +0200
commit7ee77b5d1b91adf4c683d0d871015fe68ebca9ea (patch)
tree2ecc51a3018c338eece2a707e6a79b6b199176e3 /tests
parentb4d09f3b811cf9bb73288c054557f2c2436bb37d (diff)
downloadrust-7ee77b5d1b91adf4c683d0d871015fe68ebca9ea.tar.gz
rust-7ee77b5d1b91adf4c683d0d871015fe68ebca9ea.zip
Add support for ptr::write for the invalid_reference_casting lint
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/lint/reference_casting.rs11
-rw-r--r--tests/ui/lint/reference_casting.stderr47
2 files changed, 53 insertions, 5 deletions
diff --git a/tests/ui/lint/reference_casting.rs b/tests/ui/lint/reference_casting.rs
index 6c38bca3daa..92d985948ec 100644
--- a/tests/ui/lint/reference_casting.rs
+++ b/tests/ui/lint/reference_casting.rs
@@ -71,6 +71,11 @@ unsafe fn assign_to_ref() {
     //~^ ERROR assigning to `&T` is undefined behavior
     *std::mem::transmute::<_, *mut i32>(num) += 1;
     //~^ ERROR assigning to `&T` is undefined behavior
+    std::ptr::write(
+    //~^ ERROR assigning to `&T` is undefined behavior
+        std::mem::transmute::<*const i32, *mut i32>(num),
+        -1i32,
+    );
 
     let value = num as *const i32 as *mut i32;
     *value = 1;
@@ -79,6 +84,12 @@ unsafe fn assign_to_ref() {
     //~^ ERROR assigning to `&T` is undefined behavior
     *(num as *const _ as usize as *mut i32) = 2;
     //~^ ERROR assigning to `&T` is undefined behavior
+    std::ptr::write(value, 2);
+    //~^ ERROR assigning to `&T` is undefined behavior
+    std::ptr::write_unaligned(value, 2);
+    //~^ ERROR assigning to `&T` is undefined behavior
+    std::ptr::write_volatile(value, 2);
+    //~^ ERROR assigning to `&T` is undefined behavior
 
     unsafe fn generic_assign_to_ref<T>(this: &T, a: T) {
         *(this as *const _ as *mut _) = a;
diff --git a/tests/ui/lint/reference_casting.stderr b/tests/ui/lint/reference_casting.stderr
index 7ff9b76a85e..c1589f13835 100644
--- a/tests/ui/lint/reference_casting.stderr
+++ b/tests/ui/lint/reference_casting.stderr
@@ -131,7 +131,17 @@ LL |     *std::mem::transmute::<_, *mut i32>(num) += 1;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
-  --> $DIR/reference_casting.rs:76:5
+  --> $DIR/reference_casting.rs:74:5
+   |
+LL | /     std::ptr::write(
+LL | |
+LL | |         std::mem::transmute::<*const i32, *mut i32>(num),
+LL | |         -1i32,
+LL | |     );
+   | |_____^
+
+error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
+  --> $DIR/reference_casting.rs:81:5
    |
 LL |     let value = num as *const i32 as *mut i32;
    |                 ----------------------------- casting happend here
@@ -139,22 +149,49 @@ LL |     *value = 1;
    |     ^^^^^^^^^^
 
 error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
-  --> $DIR/reference_casting.rs:78:5
+  --> $DIR/reference_casting.rs:83:5
    |
 LL |     *(num as *const i32).cast::<i32>().cast_mut() = 2;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
-  --> $DIR/reference_casting.rs:80:5
+  --> $DIR/reference_casting.rs:85:5
    |
 LL |     *(num as *const _ as usize as *mut i32) = 2;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
-  --> $DIR/reference_casting.rs:84:9
+  --> $DIR/reference_casting.rs:87:5
+   |
+LL |     let value = num as *const i32 as *mut i32;
+   |                 ----------------------------- casting happend here
+...
+LL |     std::ptr::write(value, 2);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
+  --> $DIR/reference_casting.rs:89:5
+   |
+LL |     let value = num as *const i32 as *mut i32;
+   |                 ----------------------------- casting happend here
+...
+LL |     std::ptr::write_unaligned(value, 2);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
+  --> $DIR/reference_casting.rs:91:5
+   |
+LL |     let value = num as *const i32 as *mut i32;
+   |                 ----------------------------- casting happend here
+...
+LL |     std::ptr::write_volatile(value, 2);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
+  --> $DIR/reference_casting.rs:95:9
    |
 LL |         *(this as *const _ as *mut _) = a;
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to 25 previous errors
+error: aborting due to 29 previous errors