@@ -18,6 +18,7 @@ mod fn_to_numeric_cast;
18
18
mod fn_to_numeric_cast_any;
19
19
mod fn_to_numeric_cast_with_truncation;
20
20
mod ptr_as_ptr;
21
+ mod ptr_cast_constness;
21
22
mod unnecessary_cast;
22
23
mod utils;
23
24
@@ -399,7 +400,7 @@ declare_clippy_lint! {
399
400
/// namely `*const T` to `*const U` and `*mut T` to `*mut U`.
400
401
///
401
402
/// ### Why is this bad?
402
- /// Though `as` casts between raw pointers is not terrible, `pointer::cast` is safer because
403
+ /// Though `as` casts between raw pointers are not terrible, `pointer::cast` is safer because
403
404
/// it cannot accidentally change the pointer's mutability nor cast the pointer to other types like `usize`.
404
405
///
405
406
/// ### Example
@@ -422,6 +423,34 @@ declare_clippy_lint! {
422
423
"casting using `as` from and to raw pointers that doesn't change its mutability, where `pointer::cast` could take the place of `as`"
423
424
}
424
425
426
+ declare_clippy_lint ! {
427
+ /// ### What it does
428
+ /// Checks for `as` casts between raw pointers which change its constness, namely `*const T` to
429
+ /// `*mut T` and `*mut T` to `*const T`.
430
+ ///
431
+ /// ### Why is this bad?
432
+ /// Though `as` casts between raw pointers are not terrible, `pointer::cast_mut` and
433
+ /// `pointer::cast_const` are safer because they cannot accidentally cast the pointer to another
434
+ /// type.
435
+ ///
436
+ /// ### Example
437
+ /// ```rust
438
+ /// let ptr: *const u32 = &42_u32;
439
+ /// let mut_ptr = ptr as *mut u32;
440
+ /// let ptr = mut_ptr as *const u32;
441
+ /// ```
442
+ /// Use instead:
443
+ /// ```rust
444
+ /// let ptr: *const u32 = &42_u32;
445
+ /// let mut_ptr = ptr.cast_mut();
446
+ /// let ptr = mut_ptr.cast_const();
447
+ /// ```
448
+ #[ clippy:: version = "1.65.0" ]
449
+ pub PTR_CAST_CONSTNESS ,
450
+ pedantic,
451
+ "TODO"
452
+ }
453
+
425
454
declare_clippy_lint ! {
426
455
/// ### What it does
427
456
/// Checks for casts from an enum type to an integral type which will definitely truncate the
@@ -689,6 +718,7 @@ impl_lint_pass!(Casts => [
689
718
FN_TO_NUMERIC_CAST_WITH_TRUNCATION ,
690
719
CHAR_LIT_AS_U8 ,
691
720
PTR_AS_PTR ,
721
+ PTR_CAST_CONSTNESS ,
692
722
CAST_ENUM_TRUNCATION ,
693
723
CAST_ENUM_CONSTRUCTOR ,
694
724
CAST_ABS_TO_UNSIGNED ,
@@ -722,6 +752,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
722
752
return ;
723
753
}
724
754
cast_slice_from_raw_parts:: check ( cx, expr, cast_expr, cast_to, & self . msrv ) ;
755
+ ptr_cast_constness:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
725
756
as_ptr_cast_mut:: check ( cx, expr, cast_expr, cast_to) ;
726
757
fn_to_numeric_cast_any:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
727
758
fn_to_numeric_cast:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
0 commit comments