Skip to content

Commit 52c2353

Browse files
committed
Auto merge of rust-lang#10879 - Centri3:ptr_cast_constness, r=blyxyas,xFrednet
[`ptr_cast_constness`]: Only lint on casts which don't change type fixes rust-lang#10874 changelog: [`ptr_cast_constness`]: Only lint on casts which don't change type
2 parents a97a94a + cd1d7a3 commit 52c2353

File tree

4 files changed

+67
-40
lines changed

4 files changed

+67
-40
lines changed

clippy_lints/src/casts/ptr_cast_constness.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ use rustc_middle::ty::{self, Ty, TypeAndMut};
99

1010
use super::PTR_CAST_CONSTNESS;
1111

12-
pub(super) fn check(
12+
pub(super) fn check<'tcx>(
1313
cx: &LateContext<'_>,
1414
expr: &Expr<'_>,
1515
cast_expr: &Expr<'_>,
16-
cast_from: Ty<'_>,
17-
cast_to: Ty<'_>,
16+
cast_from: Ty<'tcx>,
17+
cast_to: Ty<'tcx>,
1818
msrv: &Msrv,
1919
) {
2020
if_chain! {
2121
if msrv.meets(POINTER_CAST_CONSTNESS);
22-
if let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, .. }) = cast_from.kind();
23-
if let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind();
22+
if let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, ty: from_ty }) = cast_from.kind();
23+
if let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, ty: to_ty }) = cast_to.kind();
2424
if matches!((from_mutbl, to_mutbl),
2525
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not));
26+
if from_ty == to_ty;
2627
then {
2728
let sugg = Sugg::hir(cx, cast_expr, "_");
2829
let constness = match *to_mutbl {
@@ -34,7 +35,7 @@ pub(super) fn check(
3435
cx,
3536
PTR_CAST_CONSTNESS,
3637
expr.span,
37-
"`as` casting between raw pointers while changing its constness",
38+
"`as` casting between raw pointers while changing only its constness",
3839
&format!("try `pointer::cast_{constness}`, a safer alternative"),
3940
format!("{}.cast_{constness}()", sugg.maybe_par()),
4041
Applicability::MachineApplicable,

tests/ui/ptr_cast_constness.fixed

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
//@aux-build:proc_macros.rs
33

44
#![warn(clippy::ptr_cast_constness)]
5+
#![allow(clippy::transmute_ptr_to_ref, clippy::unnecessary_cast, unused)]
56

67
extern crate proc_macros;
78
use proc_macros::{external, inline_macros};
89

10+
unsafe fn ptr_to_ref<T, U>(p: *const T, om: *mut U) {
11+
let _: &mut T = std::mem::transmute(p.cast_mut());
12+
let _ = &mut *p.cast_mut();
13+
let _: &T = &*(om as *const T);
14+
}
15+
916
#[inline_macros]
1017
fn main() {
1118
let ptr: *const u32 = &42_u32;
1219
let mut_ptr: *mut u32 = &mut 42_u32;
1320

14-
let _ = ptr as *const i32;
15-
let _ = mut_ptr as *mut i32;
21+
let _ = ptr as *const u32;
22+
let _ = mut_ptr as *mut u32;
1623

1724
// Make sure the lint can handle the difference in their operator precedences.
1825
unsafe {
@@ -29,10 +36,10 @@ fn main() {
2936
let _ = ptr_of_array as *const dyn std::fmt::Debug;
3037

3138
// Make sure the lint is triggered inside a macro
32-
let _ = inline!($ptr as *const i32);
39+
let _ = inline!($ptr as *const u32);
3340

3441
// Do not lint inside macros from external crates
35-
let _ = external!($ptr as *const i32);
42+
let _ = external!($ptr as *const u32);
3643
}
3744

3845
#[clippy::msrv = "1.64"]
@@ -41,8 +48,8 @@ fn _msrv_1_64() {
4148
let mut_ptr: *mut u32 = &mut 42_u32;
4249

4350
// `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this
44-
let _ = ptr as *mut i32;
45-
let _ = mut_ptr as *const i32;
51+
let _ = ptr as *mut u32;
52+
let _ = mut_ptr as *const u32;
4653
}
4754

4855
#[clippy::msrv = "1.65"]

tests/ui/ptr_cast_constness.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,44 @@
22
//@aux-build:proc_macros.rs
33

44
#![warn(clippy::ptr_cast_constness)]
5+
#![allow(clippy::transmute_ptr_to_ref, clippy::unnecessary_cast, unused)]
56

67
extern crate proc_macros;
78
use proc_macros::{external, inline_macros};
89

10+
unsafe fn ptr_to_ref<T, U>(p: *const T, om: *mut U) {
11+
let _: &mut T = std::mem::transmute(p as *mut T);
12+
let _ = &mut *(p as *mut T);
13+
let _: &T = &*(om as *const T);
14+
}
15+
916
#[inline_macros]
1017
fn main() {
1118
let ptr: *const u32 = &42_u32;
1219
let mut_ptr: *mut u32 = &mut 42_u32;
1320

14-
let _ = ptr as *const i32;
15-
let _ = mut_ptr as *mut i32;
21+
let _ = ptr as *const u32;
22+
let _ = mut_ptr as *mut u32;
1623

1724
// Make sure the lint can handle the difference in their operator precedences.
1825
unsafe {
1926
let ptr_ptr: *const *const u32 = &ptr;
20-
let _ = *ptr_ptr as *mut i32;
27+
let _ = *ptr_ptr as *mut u32;
2128
}
2229

23-
let _ = ptr as *mut i32;
24-
let _ = mut_ptr as *const i32;
30+
let _ = ptr as *mut u32;
31+
let _ = mut_ptr as *const u32;
2532

2633
// Lint this, since pointer::cast_mut and pointer::cast_const have ?Sized
2734
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4];
2835
let _ = ptr_of_array as *const [u32];
2936
let _ = ptr_of_array as *const dyn std::fmt::Debug;
3037

3138
// Make sure the lint is triggered inside a macro
32-
let _ = inline!($ptr as *const i32);
39+
let _ = inline!($ptr as *const u32);
3340

3441
// Do not lint inside macros from external crates
35-
let _ = external!($ptr as *const i32);
42+
let _ = external!($ptr as *const u32);
3643
}
3744

3845
#[clippy::msrv = "1.64"]
@@ -41,15 +48,15 @@ fn _msrv_1_64() {
4148
let mut_ptr: *mut u32 = &mut 42_u32;
4249

4350
// `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this
44-
let _ = ptr as *mut i32;
45-
let _ = mut_ptr as *const i32;
51+
let _ = ptr as *mut u32;
52+
let _ = mut_ptr as *const u32;
4653
}
4754

4855
#[clippy::msrv = "1.65"]
4956
fn _msrv_1_65() {
5057
let ptr: *const u32 = &42_u32;
5158
let mut_ptr: *mut u32 = &mut 42_u32;
5259

53-
let _ = ptr as *mut i32;
54-
let _ = mut_ptr as *const i32;
60+
let _ = ptr as *mut u32;
61+
let _ = mut_ptr as *const u32;
5562
}

tests/ui/ptr_cast_constness.stderr

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
1-
error: `as` casting between raw pointers while changing its constness
2-
--> $DIR/ptr_cast_constness.rs:20:17
1+
error: `as` casting between raw pointers while changing only its constness
2+
--> $DIR/ptr_cast_constness.rs:11:41
33
|
4-
LL | let _ = *ptr_ptr as *mut i32;
5-
| ^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `(*ptr_ptr).cast_mut()`
4+
LL | let _: &mut T = std::mem::transmute(p as *mut T);
5+
| ^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `p.cast_mut()`
66
|
77
= note: `-D clippy::ptr-cast-constness` implied by `-D warnings`
88

9-
error: `as` casting between raw pointers while changing its constness
10-
--> $DIR/ptr_cast_constness.rs:23:13
9+
error: `as` casting between raw pointers while changing only its constness
10+
--> $DIR/ptr_cast_constness.rs:12:19
11+
|
12+
LL | let _ = &mut *(p as *mut T);
13+
| ^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `p.cast_mut()`
14+
15+
error: `as` casting between raw pointers while changing only its constness
16+
--> $DIR/ptr_cast_constness.rs:27:17
17+
|
18+
LL | let _ = *ptr_ptr as *mut u32;
19+
| ^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `(*ptr_ptr).cast_mut()`
20+
21+
error: `as` casting between raw pointers while changing only its constness
22+
--> $DIR/ptr_cast_constness.rs:30:13
1123
|
12-
LL | let _ = ptr as *mut i32;
24+
LL | let _ = ptr as *mut u32;
1325
| ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()`
1426

15-
error: `as` casting between raw pointers while changing its constness
16-
--> $DIR/ptr_cast_constness.rs:24:13
27+
error: `as` casting between raw pointers while changing only its constness
28+
--> $DIR/ptr_cast_constness.rs:31:13
1729
|
18-
LL | let _ = mut_ptr as *const i32;
30+
LL | let _ = mut_ptr as *const u32;
1931
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`
2032

21-
error: `as` casting between raw pointers while changing its constness
22-
--> $DIR/ptr_cast_constness.rs:53:13
33+
error: `as` casting between raw pointers while changing only its constness
34+
--> $DIR/ptr_cast_constness.rs:60:13
2335
|
24-
LL | let _ = ptr as *mut i32;
36+
LL | let _ = ptr as *mut u32;
2537
| ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()`
2638

27-
error: `as` casting between raw pointers while changing its constness
28-
--> $DIR/ptr_cast_constness.rs:54:13
39+
error: `as` casting between raw pointers while changing only its constness
40+
--> $DIR/ptr_cast_constness.rs:61:13
2941
|
30-
LL | let _ = mut_ptr as *const i32;
42+
LL | let _ = mut_ptr as *const u32;
3143
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`
3244

33-
error: aborting due to 5 previous errors
45+
error: aborting due to 7 previous errors
3446

0 commit comments

Comments
 (0)