about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorFabian Wolff <fabian.wolff@alumni.ethz.ch>2021-10-04 21:15:02 +0200
committerFabian Wolff <fabian.wolff@alumni.ethz.ch>2021-10-04 21:15:02 +0200
commit388bcc1cb086c567f17a4933bfb1d3f14eb19fb7 (patch)
treed9c2793c032205ab5e9862a8734f55c41ba9d772 /src/test
parentedebf77e0090195bf80c0d8cda821e1bf9d03053 (diff)
downloadrust-388bcc1cb086c567f17a4933bfb1d3f14eb19fb7.tar.gz
rust-388bcc1cb086c567f17a4933bfb1d3f14eb19fb7.zip
Fix suggestion to borrow when casting from pointer to reference
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/cast/issue-89497.fixed10
-rw-r--r--src/test/ui/cast/issue-89497.rs10
-rw-r--r--src/test/ui/cast/issue-89497.stderr15
-rw-r--r--src/test/ui/error-codes/E0605.stderr5
-rw-r--r--src/test/ui/issues/issue-2995.stderr5
-rw-r--r--src/test/ui/mismatched_types/cast-rfc0401.stderr5
6 files changed, 44 insertions, 6 deletions
diff --git a/src/test/ui/cast/issue-89497.fixed b/src/test/ui/cast/issue-89497.fixed
new file mode 100644
index 00000000000..04c10a5f79e
--- /dev/null
+++ b/src/test/ui/cast/issue-89497.fixed
@@ -0,0 +1,10 @@
+// Regression test for issue #89497.
+
+// run-rustfix
+
+fn main() {
+    let pointer: usize = &1_i32 as *const i32 as usize;
+    let _reference: &'static i32 = unsafe { &*(pointer as *const i32) };
+    //~^ ERROR: non-primitive cast
+    //~| HELP: consider borrowing the value
+}
diff --git a/src/test/ui/cast/issue-89497.rs b/src/test/ui/cast/issue-89497.rs
new file mode 100644
index 00000000000..76301b704c8
--- /dev/null
+++ b/src/test/ui/cast/issue-89497.rs
@@ -0,0 +1,10 @@
+// Regression test for issue #89497.
+
+// run-rustfix
+
+fn main() {
+    let pointer: usize = &1_i32 as *const i32 as usize;
+    let _reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
+    //~^ ERROR: non-primitive cast
+    //~| HELP: consider borrowing the value
+}
diff --git a/src/test/ui/cast/issue-89497.stderr b/src/test/ui/cast/issue-89497.stderr
new file mode 100644
index 00000000000..3726f8a4101
--- /dev/null
+++ b/src/test/ui/cast/issue-89497.stderr
@@ -0,0 +1,15 @@
+error[E0605]: non-primitive cast: `*const i32` as `&'static i32`
+  --> $DIR/issue-89497.rs:7:45
+   |
+LL |     let _reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
+   |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
+   |
+help: consider borrowing the value
+   |
+LL -     let _reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
+LL +     let _reference: &'static i32 = unsafe { &*(pointer as *const i32) };
+   | 
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0605`.
diff --git a/src/test/ui/error-codes/E0605.stderr b/src/test/ui/error-codes/E0605.stderr
index e5647ee6d09..d082b6c10cc 100644
--- a/src/test/ui/error-codes/E0605.stderr
+++ b/src/test/ui/error-codes/E0605.stderr
@@ -12,8 +12,9 @@ LL |     v as &u8;
    |
 help: consider borrowing the value
    |
-LL |     &*v as &u8;
-   |     ++
+LL -     v as &u8;
+LL +     &*v;
+   | 
 
 error: aborting due to 2 previous errors
 
diff --git a/src/test/ui/issues/issue-2995.stderr b/src/test/ui/issues/issue-2995.stderr
index 455d32e233a..7616f987d73 100644
--- a/src/test/ui/issues/issue-2995.stderr
+++ b/src/test/ui/issues/issue-2995.stderr
@@ -6,8 +6,9 @@ LL |     let _q: &isize = p as &isize;
    |
 help: consider borrowing the value
    |
-LL |     let _q: &isize = &*p as &isize;
-   |                      ++
+LL -     let _q: &isize = p as &isize;
+LL +     let _q: &isize = &*p;
+   | 
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/mismatched_types/cast-rfc0401.stderr b/src/test/ui/mismatched_types/cast-rfc0401.stderr
index a47eb87cff0..7f91d5ed42c 100644
--- a/src/test/ui/mismatched_types/cast-rfc0401.stderr
+++ b/src/test/ui/mismatched_types/cast-rfc0401.stderr
@@ -28,8 +28,9 @@ LL |     let _ = v as &u8;
    |
 help: consider borrowing the value
    |
-LL |     let _ = &*v as &u8;
-   |             ++
+LL -     let _ = v as &u8;
+LL +     let _ = &*v;
+   | 
 
 error[E0605]: non-primitive cast: `*const u8` as `E`
   --> $DIR/cast-rfc0401.rs:30:13