Skip to content

Commit

Permalink
Auto merge of #10879 - Centri3:ptr_cast_constness, r=blyxyas,xFrednet
Browse files Browse the repository at this point in the history
[`ptr_cast_constness`]: Only lint on casts which don't change type

fixes #10874

changelog: [`ptr_cast_constness`]: Only lint on casts which don't change type
  • Loading branch information
bors committed Jun 3, 2023
2 parents a97a94a + cd1d7a3 commit 52c2353
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 40 deletions.
13 changes: 7 additions & 6 deletions clippy_lints/src/casts/ptr_cast_constness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ use rustc_middle::ty::{self, Ty, TypeAndMut};

use super::PTR_CAST_CONSTNESS;

pub(super) fn check(
pub(super) fn check<'tcx>(
cx: &LateContext<'_>,
expr: &Expr<'_>,
cast_expr: &Expr<'_>,
cast_from: Ty<'_>,
cast_to: Ty<'_>,
cast_from: Ty<'tcx>,
cast_to: Ty<'tcx>,
msrv: &Msrv,
) {
if_chain! {
if msrv.meets(POINTER_CAST_CONSTNESS);
if let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, .. }) = cast_from.kind();
if let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind();
if let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, ty: from_ty }) = cast_from.kind();
if let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, ty: to_ty }) = cast_to.kind();
if matches!((from_mutbl, to_mutbl),
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not));
if from_ty == to_ty;
then {
let sugg = Sugg::hir(cx, cast_expr, "_");
let constness = match *to_mutbl {
Expand All @@ -34,7 +35,7 @@ pub(super) fn check(
cx,
PTR_CAST_CONSTNESS,
expr.span,
"`as` casting between raw pointers while changing its constness",
"`as` casting between raw pointers while changing only its constness",
&format!("try `pointer::cast_{constness}`, a safer alternative"),
format!("{}.cast_{constness}()", sugg.maybe_par()),
Applicability::MachineApplicable,
Expand Down
19 changes: 13 additions & 6 deletions tests/ui/ptr_cast_constness.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
//@aux-build:proc_macros.rs

#![warn(clippy::ptr_cast_constness)]
#![allow(clippy::transmute_ptr_to_ref, clippy::unnecessary_cast, 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 {
Expand All @@ -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"]
Expand All @@ -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"]
Expand Down
29 changes: 18 additions & 11 deletions tests/ui/ptr_cast_constness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,44 @@
//@aux-build:proc_macros.rs

#![warn(clippy::ptr_cast_constness)]
#![allow(clippy::transmute_ptr_to_ref, clippy::unnecessary_cast, 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];
let _ = ptr_of_array as *const [u32];
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"]
Expand All @@ -41,15 +48,15 @@ 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"]
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;
}
46 changes: 29 additions & 17 deletions tests/ui/ptr_cast_constness.stderr
Original file line number Diff line number Diff line change
@@ -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 _ = &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 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: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

0 comments on commit 52c2353

Please sign in to comment.