Skip to content

Commit ca93ebd

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 ed60394 commit ca93ebd

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,33 @@ pub(super) fn check<'tcx>(
8282
}
8383
}
8484
}
85+
86+
pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
87+
if let ExprKind::MethodCall(method, cast_expr, [], _) = expr.kind
88+
&& let ExprKind::Call(func, []) = cast_expr.kind
89+
&& let ExprKind::Path(QPath::Resolved(None, path)) = func.kind
90+
&& let Some(defid) = path.res.opt_def_id()
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 = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app).to_string();
98+
let Some((_, after_lt)) = sugg.split_once("::<") else {
99+
return;
100+
};
101+
let method = if mutable { "null_mut" } else { "null" };
102+
let prefix = std_or_core(cx).unwrap_or("::core");
103+
span_lint_and_sugg(
104+
cx,
105+
PTR_CAST_CONSTNESS,
106+
expr.span,
107+
"changing constness of a null pointer",
108+
format!("use `{method}()` directly instead"),
109+
format!("{prefix}::ptr::{method}::<{after_lt}"),
110+
app,
111+
);
112+
}
113+
}
114+
}

tests/ui/ptr_cast_constness.fixed

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 22 additions & 2 deletions
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)