-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
9 changed files
with
133 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|