only lint when cast_from and cast_to's ty are the same

This commit is contained in:
Centri3 2023-06-02 13:14:16 -05:00
parent 00001d6e08
commit ad7c44b3e4
4 changed files with 67 additions and 40 deletions

View file

@ -2,17 +2,24 @@
//@aux-build:proc_macros.rs
#![warn(clippy::ptr_cast_constness)]
#![allow(clippy::transmute_ptr_to_ref, unused)]
extern crate proc_macros;
use proc_macros::{external, inline_macros};
unsafe fn ptr_to_ref<T, U>(p: *const T, om: *mut U) {
let _: &mut T = std::mem::transmute(p.cast_mut());
let _ = &mut *p.cast_mut();
let _: &T = &*(om as *const T);
}
#[inline_macros]
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;
let _ = ptr as *const u32;
let _ = mut_ptr as *mut u32;
// Make sure the lint can handle the difference in their operator precedences.
unsafe {
@ -29,10 +36,10 @@ fn main() {
let _ = ptr_of_array as *const dyn std::fmt::Debug;
// Make sure the lint is triggered inside a macro
let _ = inline!($ptr as *const i32);
let _ = inline!($ptr as *const u32);
// Do not lint inside macros from external crates
let _ = external!($ptr as *const i32);
let _ = external!($ptr as *const u32);
}
#[clippy::msrv = "1.64"]
@ -41,8 +48,8 @@ fn _msrv_1_64() {
let mut_ptr: *mut u32 = &mut 42_u32;
// `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this
let _ = ptr as *mut i32;
let _ = mut_ptr as *const i32;
let _ = ptr as *mut u32;
let _ = mut_ptr as *const u32;
}
#[clippy::msrv = "1.65"]

View file

@ -2,26 +2,33 @@
//@aux-build:proc_macros.rs
#![warn(clippy::ptr_cast_constness)]
#![allow(clippy::transmute_ptr_to_ref, unused)]
extern crate proc_macros;
use proc_macros::{external, inline_macros};
unsafe fn ptr_to_ref<T, U>(p: *const T, om: *mut U) {
let _: &mut T = std::mem::transmute(p as *mut T);
let _ = &mut *(p as *mut T);
let _: &T = &*(om as *const T);
}
#[inline_macros]
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;
let _ = ptr as *const u32;
let _ = mut_ptr as *mut u32;
// Make sure the lint can handle the difference in their operator precedences.
unsafe {
let ptr_ptr: *const *const u32 = &ptr;
let _ = *ptr_ptr as *mut i32;
let _ = *ptr_ptr as *mut u32;
}
let _ = ptr as *mut i32;
let _ = mut_ptr as *const i32;
let _ = ptr as *mut u32;
let _ = mut_ptr as *const u32;
// Lint this, since pointer::cast_mut and pointer::cast_const have ?Sized
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4];
@ -29,10 +36,10 @@ fn main() {
let _ = ptr_of_array as *const dyn std::fmt::Debug;
// Make sure the lint is triggered inside a macro
let _ = inline!($ptr as *const i32);
let _ = inline!($ptr as *const u32);
// Do not lint inside macros from external crates
let _ = external!($ptr as *const i32);
let _ = external!($ptr as *const u32);
}
#[clippy::msrv = "1.64"]
@ -41,8 +48,8 @@ fn _msrv_1_64() {
let mut_ptr: *mut u32 = &mut 42_u32;
// `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this
let _ = ptr as *mut i32;
let _ = mut_ptr as *const i32;
let _ = ptr as *mut u32;
let _ = mut_ptr as *const u32;
}
#[clippy::msrv = "1.65"]
@ -50,6 +57,6 @@ fn _msrv_1_65() {
let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
let _ = ptr as *mut i32;
let _ = mut_ptr as *const i32;
let _ = ptr as *mut u32;
let _ = mut_ptr as *const u32;
}

View file

@ -1,34 +1,46 @@
error: `as` casting between raw pointers while changing its constness
--> $DIR/ptr_cast_constness.rs:20:17
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:11:41
|
LL | let _ = *ptr_ptr as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `(*ptr_ptr).cast_mut()`
LL | let _: &mut T = std::mem::transmute(p as *mut T);
| ^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `p.cast_mut()`
|
= note: `-D clippy::ptr-cast-constness` implied by `-D warnings`
error: `as` casting between raw pointers while changing its constness
--> $DIR/ptr_cast_constness.rs:23:13
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:12:19
|
LL | let _ = ptr as *mut i32;
LL | let _ = &mut *(p as *mut T);
| ^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `p.cast_mut()`
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:27:17
|
LL | let _ = *ptr_ptr as *mut u32;
| ^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `(*ptr_ptr).cast_mut()`
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:30:13
|
LL | let _ = ptr as *mut u32;
| ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()`
error: `as` casting between raw pointers while changing its constness
--> $DIR/ptr_cast_constness.rs:24:13
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:31:13
|
LL | let _ = mut_ptr as *const i32;
LL | let _ = mut_ptr as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`
error: `as` casting between raw pointers while changing its constness
--> $DIR/ptr_cast_constness.rs:53:13
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:60:13
|
LL | let _ = ptr as *mut i32;
LL | let _ = ptr as *mut u32;
| ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()`
error: `as` casting between raw pointers while changing its constness
--> $DIR/ptr_cast_constness.rs:54:13
error: `as` casting between raw pointers while changing only its constness
--> $DIR/ptr_cast_constness.rs:61:13
|
LL | let _ = mut_ptr as *const i32;
LL | let _ = mut_ptr as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`
error: aborting due to 5 previous errors
error: aborting due to 7 previous errors