forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#6542 - rail-rain:ptr_as_ptr, r=flip1995
Add a new lint `ptr_as_ptr` This PR adds a new lint `ptr_as_ptr` which checks for `as` casts between raw pointers without changing its mutability and suggest replacing it with `pointer::cast`. Closes rust-lang#5890. Open question: should this lint be `pedantic` or `style`? I set it `pedantic` for now because the original post suggests using it, but I think the lint also fits well to `style`. --- changelog: New lint `ptr_as_ptr`
- Loading branch information
Showing
6 changed files
with
247 additions
and
4 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,50 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::ptr_as_ptr)] | ||
#![feature(custom_inner_attributes)] | ||
|
||
fn main() { | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr.cast::<i32>(); | ||
let _ = mut_ptr.cast::<i32>(); | ||
|
||
// Make sure the lint can handle the difference in their operator precedences. | ||
unsafe { | ||
let ptr_ptr: *const *const u32 = &ptr; | ||
let _ = (*ptr_ptr).cast::<i32>(); | ||
} | ||
|
||
// Changes in mutability. Do not lint this. | ||
let _ = ptr as *mut i32; | ||
let _ = mut_ptr as *const i32; | ||
|
||
// `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this. | ||
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4]; | ||
let _ = ptr_of_array as *const [u32]; | ||
let _ = ptr_of_array as *const dyn std::fmt::Debug; | ||
|
||
// Ensure the lint doesn't produce unnecessary turbofish for inferred types. | ||
let _: *const i32 = ptr.cast(); | ||
let _: *mut i32 = mut_ptr.cast(); | ||
} | ||
|
||
fn _msrv_1_37() { | ||
#![clippy::msrv = "1.37"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
// `pointer::cast` was stabilized in 1.38. Do not lint this | ||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
} | ||
|
||
fn _msrv_1_38() { | ||
#![clippy::msrv = "1.38"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr.cast::<i32>(); | ||
let _ = mut_ptr.cast::<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,50 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::ptr_as_ptr)] | ||
#![feature(custom_inner_attributes)] | ||
|
||
fn main() { | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
|
||
// Make sure the lint can handle the difference in their operator precedences. | ||
unsafe { | ||
let ptr_ptr: *const *const u32 = &ptr; | ||
let _ = *ptr_ptr as *const i32; | ||
} | ||
|
||
// Changes in mutability. Do not lint this. | ||
let _ = ptr as *mut i32; | ||
let _ = mut_ptr as *const i32; | ||
|
||
// `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this. | ||
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4]; | ||
let _ = ptr_of_array as *const [u32]; | ||
let _ = ptr_of_array as *const dyn std::fmt::Debug; | ||
|
||
// Ensure the lint doesn't produce unnecessary turbofish for inferred types. | ||
let _: *const i32 = ptr as *const _; | ||
let _: *mut i32 = mut_ptr as _; | ||
} | ||
|
||
fn _msrv_1_37() { | ||
#![clippy::msrv = "1.37"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
// `pointer::cast` was stabilized in 1.38. Do not lint this | ||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
} | ||
|
||
fn _msrv_1_38() { | ||
#![clippy::msrv = "1.38"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr as *const i32; | ||
let _ = mut_ptr 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,46 @@ | ||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:10:13 | ||
| | ||
LL | let _ = ptr as *const i32; | ||
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()` | ||
| | ||
= note: `-D clippy::ptr-as-ptr` implied by `-D warnings` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:11:13 | ||
| | ||
LL | let _ = mut_ptr as *mut i32; | ||
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:16:17 | ||
| | ||
LL | let _ = *ptr_ptr as *const i32; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(*ptr_ptr).cast::<i32>()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:29:25 | ||
| | ||
LL | let _: *const i32 = ptr as *const _; | ||
| ^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:30:23 | ||
| | ||
LL | let _: *mut i32 = mut_ptr as _; | ||
| ^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:48:13 | ||
| | ||
LL | let _ = ptr as *const i32; | ||
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:49:13 | ||
| | ||
LL | let _ = mut_ptr as *mut i32; | ||
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()` | ||
|
||
error: aborting due to 7 previous errors | ||
|