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
This commit is contained in:
Jason Newcomb 2024-12-05 10:18:19 +00:00 committed by GitHub
commit a5e46a6b08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 133 additions and 19 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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