about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authordswij <dharmasw@outlook.com>2025-04-21 12:38:11 +0000
committerGitHub <noreply@github.com>2025-04-21 12:38:11 +0000
commitf829f8cd5b604c0a792e10f2e77050099cdd4a0f (patch)
treedc1d695660fc9f82d5c2a80d9031d67245df115d /tests
parent48430c29700834606152ab85a307fe3e93cea182 (diff)
parent78d456a647c73bfdf933b806080e6e1a06b37c06 (diff)
downloadrust-f829f8cd5b604c0a792e10f2e77050099cdd4a0f.tar.gz
rust-f829f8cd5b604c0a792e10f2e77050099cdd4a0f.zip
Make `borrow_as_ptr` flag implicit casts as well (#14408)
While I like replacing `&x` by `&raw const x` and `&mut x` by `&raw mut
x`, I'm less sure I like the suggested reborrows such as `&raw const
*p`. There was one in Clippy sources, see the PR diff.

@RalfJung, any opinion on this?

Fix #14406

changelog: [`borrow_as_ptr`]: lint implicit casts as well
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/borrow_as_ptr.fixed18
-rw-r--r--tests/ui/borrow_as_ptr.rs18
-rw-r--r--tests/ui/borrow_as_ptr.stderr35
3 files changed, 70 insertions, 1 deletions
diff --git a/tests/ui/borrow_as_ptr.fixed b/tests/ui/borrow_as_ptr.fixed
index 3dca06fce4b..3ba2eea59f0 100644
--- a/tests/ui/borrow_as_ptr.fixed
+++ b/tests/ui/borrow_as_ptr.fixed
@@ -29,3 +29,21 @@ fn issue_13882() {
     let _raw = (&raw mut x[1]).wrapping_offset(-1);
     //~^ borrow_as_ptr
 }
+
+fn implicit_cast() {
+    let val = 1;
+    let p: *const i32 = &raw const val;
+    //~^ borrow_as_ptr
+
+    let mut val = 1;
+    let p: *mut i32 = &raw mut val;
+    //~^ borrow_as_ptr
+
+    let mut val = 1;
+    // Only lint the leftmost argument, the rightmost is ref to a temporary
+    core::ptr::eq(&raw const val, &1);
+    //~^ borrow_as_ptr
+
+    // Do not lint references to temporaries
+    core::ptr::eq(&0i32, &1i32);
+}
diff --git a/tests/ui/borrow_as_ptr.rs b/tests/ui/borrow_as_ptr.rs
index 3559dc23d01..8cdd0512da5 100644
--- a/tests/ui/borrow_as_ptr.rs
+++ b/tests/ui/borrow_as_ptr.rs
@@ -29,3 +29,21 @@ fn issue_13882() {
     let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
     //~^ borrow_as_ptr
 }
+
+fn implicit_cast() {
+    let val = 1;
+    let p: *const i32 = &val;
+    //~^ borrow_as_ptr
+
+    let mut val = 1;
+    let p: *mut i32 = &mut val;
+    //~^ borrow_as_ptr
+
+    let mut val = 1;
+    // Only lint the leftmost argument, the rightmost is ref to a temporary
+    core::ptr::eq(&val, &1);
+    //~^ borrow_as_ptr
+
+    // Do not lint references to temporaries
+    core::ptr::eq(&0i32, &1i32);
+}
diff --git a/tests/ui/borrow_as_ptr.stderr b/tests/ui/borrow_as_ptr.stderr
index 4a9f2ed4aa0..b1fcce49403 100644
--- a/tests/ui/borrow_as_ptr.stderr
+++ b/tests/ui/borrow_as_ptr.stderr
@@ -25,5 +25,38 @@ error: borrow as raw pointer
 LL |     let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
    |                 ^^^^^^^^^^^^^^^^^^^^^ help: try: `&raw mut x[1]`
 
-error: aborting due to 4 previous errors
+error: implicit borrow as raw pointer
+  --> tests/ui/borrow_as_ptr.rs:35:25
+   |
+LL |     let p: *const i32 = &val;
+   |                         ^^^^
+   |
+help: use a raw pointer instead
+   |
+LL |     let p: *const i32 = &raw const val;
+   |                          +++++++++
+
+error: implicit borrow as raw pointer
+  --> tests/ui/borrow_as_ptr.rs:39:23
+   |
+LL |     let p: *mut i32 = &mut val;
+   |                       ^^^^^^^^
+   |
+help: use a raw pointer instead
+   |
+LL |     let p: *mut i32 = &raw mut val;
+   |                        +++
+
+error: implicit borrow as raw pointer
+  --> tests/ui/borrow_as_ptr.rs:44:19
+   |
+LL |     core::ptr::eq(&val, &1);
+   |                   ^^^^
+   |
+help: use a raw pointer instead
+   |
+LL |     core::ptr::eq(&raw const val, &1);
+   |                    +++++++++
+
+error: aborting due to 7 previous errors