Skip to content

Commit 0059d8a

Browse files
committed
Handle null pointer constness cast through methods
This covers two cases: - `core::ptr::null::<T>().cast_mut()` -> `core::ptr::null_mut::<T>()` - `core::ptr::null_mut::<T>().cast_const()` -> `core::ptr::null::<T>()`
1 parent f0d0c8a commit 0059d8a

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

clippy_lints/src/casts/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
813813
char_lit_as_u8::check(cx, expr);
814814
ptr_as_ptr::check(cx, expr, &self.msrv);
815815
cast_slice_different_sizes::check(cx, expr, &self.msrv);
816+
ptr_cast_constness::check_null_ptr_cast_method(cx, expr);
816817
}
817818

818819
extract_msrv_attr!(LateContext);

clippy_lints/src/casts/ptr_cast_constness.rs

+31
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,34 @@ pub(super) fn check<'tcx>(
8080
}
8181
}
8282
}
83+
84+
pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
85+
if let ExprKind::MethodCall(method, cast_expr, [], _) = expr.kind
86+
&& matches!(method.ident.as_str(), "cast_mut" | "cast_const")
87+
&& let ExprKind::Call(func, []) = cast_expr.kind
88+
&& let ExprKind::Path(QPath::Resolved(None, path)) = func.kind
89+
&& let Some(defid) = path.res.opt_def_id()
90+
&& let Some(prefix) = std_or_core(cx)
91+
{
92+
let mutable = method.ident.as_str() == "cast_mut";
93+
if (cx.tcx.is_diagnostic_item(sym::ptr_null, defid) && mutable)
94+
|| (cx.tcx.is_diagnostic_item(sym::ptr_null_mut, defid) && method.ident.as_str() == "cast_const")
95+
{
96+
let mut app = Applicability::MachineApplicable;
97+
let sugg = format!("{}", Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app));
98+
let Some((_, after_lt)) = sugg.split_once("::<") else {
99+
return;
100+
};
101+
let method = if mutable { "null_mut" } else { "null" };
102+
span_lint_and_sugg(
103+
cx,
104+
PTR_CAST_CONSTNESS,
105+
expr.span,
106+
"changing constness of a null pointer",
107+
format!("use `{method}()` directly instead"),
108+
format!("{prefix}::ptr::{method}::<{after_lt}"),
109+
app,
110+
);
111+
}
112+
}
113+
}

tests/ui/ptr_cast_constness.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ fn null_pointers() {
7474
use std::ptr;
7575
let _ = std::ptr::null_mut::<String>();
7676
let _ = std::ptr::null::<u32>();
77+
let _ = std::ptr::null_mut::<u32>();
78+
let _ = std::ptr::null::<u32>();
7779

7880
// Make sure the lint is triggered inside a macro
7981
let _ = inline!(std::ptr::null_mut::<u32>());
82+
let _ = inline!(std::ptr::null_mut::<u32>());
8083

8184
// Do not lint inside macros from external crates
8285
let _ = external!(ptr::null::<u32>() as *mut u32);
86+
let _ = external!(ptr::null::<u32>().cast_mut());
8387
}

tests/ui/ptr_cast_constness.rs

+4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ fn null_pointers() {
7474
use std::ptr;
7575
let _ = ptr::null::<String>() as *mut String;
7676
let _ = ptr::null_mut::<u32>() as *const u32;
77+
let _ = ptr::null::<u32>().cast_mut();
78+
let _ = ptr::null_mut::<u32>().cast_const();
7779

7880
// Make sure the lint is triggered inside a macro
7981
let _ = inline!(ptr::null::<u32>() as *mut u32);
82+
let _ = inline!(ptr::null::<u32>().cast_mut());
8083

8184
// Do not lint inside macros from external crates
8285
let _ = external!(ptr::null::<u32>() as *mut u32);
86+
let _ = external!(ptr::null::<u32>().cast_mut());
8387
}

tests/ui/ptr_cast_constness.stderr

+22-2
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,33 @@ error: `as` casting to make a mutable null pointer into an immutable null pointe
5555
LL | let _ = ptr::null_mut::<u32>() as *const u32;
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `null()` directly instead: `std::ptr::null::<u32>()`
5757

58+
error: changing constness of a null pointer
59+
--> tests/ui/ptr_cast_constness.rs:77:13
60+
|
61+
LL | let _ = ptr::null::<u32>().cast_mut();
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `null_mut()` directly instead: `std::ptr::null_mut::<u32>()`
63+
64+
error: changing constness of a null pointer
65+
--> tests/ui/ptr_cast_constness.rs:78:13
66+
|
67+
LL | let _ = ptr::null_mut::<u32>().cast_const();
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `null()` directly instead: `std::ptr::null::<u32>()`
69+
5870
error: `as` casting to make a const null pointer into a mutable null pointer
59-
--> tests/ui/ptr_cast_constness.rs:79:21
71+
--> tests/ui/ptr_cast_constness.rs:81:21
6072
|
6173
LL | let _ = inline!(ptr::null::<u32>() as *mut u32);
6274
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `null_mut()` directly instead: `std::ptr::null_mut::<u32>()`
6375
|
6476
= note: this error originates in the macro `__inline_mac_fn_null_pointers` (in Nightly builds, run with -Z macro-backtrace for more info)
6577

66-
error: aborting due to 10 previous errors
78+
error: changing constness of a null pointer
79+
--> tests/ui/ptr_cast_constness.rs:82:21
80+
|
81+
LL | let _ = inline!(ptr::null::<u32>().cast_mut());
82+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `null_mut()` directly instead: `std::ptr::null_mut::<u32>()`
83+
|
84+
= note: this error originates in the macro `__inline_mac_fn_null_pointers` (in Nightly builds, run with -Z macro-backtrace for more info)
85+
86+
error: aborting due to 13 previous errors
6787

0 commit comments

Comments
 (0)