about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2024-12-05 10:18:19 +0000
committerGitHub <noreply@github.com>2024-12-05 10:18:19 +0000
commita5e46a6b080f77035a7ccfab99e42d109a112862 (patch)
tree47464caf54ea75257be7638bf31f26d1339f4107 /tests
parent25505302665a707bedee68ca1f3faf2a09f12c00 (diff)
parent9925f999f6a3570a251aed2039bdb6d90de5b576 (diff)
downloadrust-a5e46a6b080f77035a7ccfab99e42d109a112862.tar.gz
rust-a5e46a6b080f77035a7ccfab99e42d109a112862.zip
update `borrow_as_ptr` to suggest `&raw` syntax (#13689)
This PR updates the `borrow_as_ptr` lint to no longer suggest `addr_of!`
and `addr_of_mut!` and instead use the preferred `&raw const` and `&raw
mut` syntax.

Not sure about two things:
1. Do I need to set or update a MSRV for the lint anywhere?
2. There is a `borrow_as_ptr_no_std` test as well as a `borrow_as_ptr`
test. They used to be more relevant as the lint needed to select `std`
or `core`, but that is gone now, so maybe the `borrow_as_ptr_no_std`
should be deleted?

changelog: update `borrow_as_ptr` to suggest `&raw` syntax
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/borrow_and_ref_as_ptr.fixed11
-rw-r--r--tests/ui/borrow_and_ref_as_ptr.rs11
-rw-r--r--tests/ui/borrow_and_ref_as_ptr.stderr17
-rw-r--r--tests/ui/borrow_as_ptr_raw_ref.fixed19
-rw-r--r--tests/ui/borrow_as_ptr_raw_ref.rs19
-rw-r--r--tests/ui/borrow_as_ptr_raw_ref.stderr17
6 files changed, 94 insertions, 0 deletions
diff --git a/tests/ui/borrow_and_ref_as_ptr.fixed b/tests/ui/borrow_and_ref_as_ptr.fixed
new file mode 100644
index 00000000000..2950b158deb
--- /dev/null
+++ b/tests/ui/borrow_and_ref_as_ptr.fixed
@@ -0,0 +1,11 @@
+// Make sure that `ref_as_ptr` is not emitted when `borrow_as_ptr` is.
+
+#![warn(clippy::ref_as_ptr, clippy::borrow_as_ptr)]
+
+fn f<T>(_: T) {}
+
+fn main() {
+    let mut val = 0;
+    f(&raw const val);
+    f(&raw mut val);
+}
diff --git a/tests/ui/borrow_and_ref_as_ptr.rs b/tests/ui/borrow_and_ref_as_ptr.rs
new file mode 100644
index 00000000000..19eb8f29233
--- /dev/null
+++ b/tests/ui/borrow_and_ref_as_ptr.rs
@@ -0,0 +1,11 @@
+// Make sure that `ref_as_ptr` is not emitted when `borrow_as_ptr` is.
+
+#![warn(clippy::ref_as_ptr, clippy::borrow_as_ptr)]
+
+fn f<T>(_: T) {}
+
+fn main() {
+    let mut val = 0;
+    f(&val as *const _);
+    f(&mut val as *mut i32);
+}
diff --git a/tests/ui/borrow_and_ref_as_ptr.stderr b/tests/ui/borrow_and_ref_as_ptr.stderr
new file mode 100644
index 00000000000..82a27af303c
--- /dev/null
+++ b/tests/ui/borrow_and_ref_as_ptr.stderr
@@ -0,0 +1,17 @@
+error: borrow as raw pointer
+  --> tests/ui/borrow_and_ref_as_ptr.rs:9:7
+   |
+LL |     f(&val as *const _);
+   |       ^^^^^^^^^^^^^^^^ help: try: `&raw const val`
+   |
+   = note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
+
+error: borrow as raw pointer
+  --> tests/ui/borrow_and_ref_as_ptr.rs:10:7
+   |
+LL |     f(&mut val as *mut i32);
+   |       ^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut val`
+
+error: aborting due to 2 previous errors
+
diff --git a/tests/ui/borrow_as_ptr_raw_ref.fixed b/tests/ui/borrow_as_ptr_raw_ref.fixed
new file mode 100644
index 00000000000..d6842e60a3e
--- /dev/null
+++ b/tests/ui/borrow_as_ptr_raw_ref.fixed
@@ -0,0 +1,19 @@
+#![warn(clippy::borrow_as_ptr)]
+#![allow(clippy::useless_vec)]
+
+fn a() -> i32 {
+    0
+}
+
+#[clippy::msrv = "1.82"]
+fn main() {
+    let val = 1;
+    let _p = &raw const val;
+    let _p = &0 as *const i32;
+    let _p = &a() as *const i32;
+    let vec = vec![1];
+    let _p = &vec.len() as *const usize;
+
+    let mut val_mut = 1;
+    let _p_mut = &raw mut val_mut;
+}
diff --git a/tests/ui/borrow_as_ptr_raw_ref.rs b/tests/ui/borrow_as_ptr_raw_ref.rs
new file mode 100644
index 00000000000..3c9daed18f1
--- /dev/null
+++ b/tests/ui/borrow_as_ptr_raw_ref.rs
@@ -0,0 +1,19 @@
+#![warn(clippy::borrow_as_ptr)]
+#![allow(clippy::useless_vec)]
+
+fn a() -> i32 {
+    0
+}
+
+#[clippy::msrv = "1.82"]
+fn main() {
+    let val = 1;
+    let _p = &val as *const i32;
+    let _p = &0 as *const i32;
+    let _p = &a() as *const i32;
+    let vec = vec![1];
+    let _p = &vec.len() as *const usize;
+
+    let mut val_mut = 1;
+    let _p_mut = &mut val_mut as *mut i32;
+}
diff --git a/tests/ui/borrow_as_ptr_raw_ref.stderr b/tests/ui/borrow_as_ptr_raw_ref.stderr
new file mode 100644
index 00000000000..5611fcae8d4
--- /dev/null
+++ b/tests/ui/borrow_as_ptr_raw_ref.stderr
@@ -0,0 +1,17 @@
+error: borrow as raw pointer
+  --> tests/ui/borrow_as_ptr_raw_ref.rs:11:14
+   |
+LL |     let _p = &val as *const i32;
+   |              ^^^^^^^^^^^^^^^^^^ help: try: `&raw const val`
+   |
+   = note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
+
+error: borrow as raw pointer
+  --> tests/ui/borrow_as_ptr_raw_ref.rs:18:18
+   |
+LL |     let _p_mut = &mut val_mut as *mut i32;
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut val_mut`
+
+error: aborting due to 2 previous errors
+