Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ljgermain committed Sep 24, 2024
1 parent 11363c7 commit 31bbe2c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
&& let Some(name) = cx.tcx.get_diagnostic_name(fun_def_id)
{
// TODO: `ptr_slice_from_raw_parts` and its mutable variant should probably still be linted
// conditionally based on how the return value is used, but not universally like the other
// functions since there are valid uses for null slice pointers.
//
// See: https://github.com/rust-lang/rust-clippy/pull/13452/files#r1773772034

// `arg` positions where null would cause U.B.
let arg_indices: &[_] = match name {
sym::ptr_read
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/invalid_null_ptr_usage.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fn main() {
let _a: A = std::ptr::read_volatile(core::ptr::NonNull::dangling().as_ptr());

let _a: A = std::ptr::replace(core::ptr::NonNull::dangling().as_ptr(), A);
let _slice: *const [usize] = std::ptr::slice_from_raw_parts(std::ptr::null_mut(), 0); // shouldn't lint
let _slice: *const [usize] = std::ptr::slice_from_raw_parts_mut(std::ptr::null_mut(), 0);

std::ptr::swap::<A>(core::ptr::NonNull::dangling().as_ptr(), &mut A);
std::ptr::swap::<A>(&mut A, core::ptr::NonNull::dangling().as_ptr());
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/invalid_null_ptr_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fn main() {
let _a: A = std::ptr::read_volatile(std::ptr::null_mut());

let _a: A = std::ptr::replace(std::ptr::null_mut(), A);
let _slice: *const [usize] = std::ptr::slice_from_raw_parts(std::ptr::null_mut(), 0); // shouldn't lint
let _slice: *const [usize] = std::ptr::slice_from_raw_parts_mut(std::ptr::null_mut(), 0);

std::ptr::swap::<A>(std::ptr::null_mut(), &mut A);
std::ptr::swap::<A>(&mut A, std::ptr::null_mut());
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/invalid_null_ptr_usage.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -85,49 +85,49 @@ LL | let _a: A = std::ptr::replace(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:28:29
--> tests/ui/invalid_null_ptr_usage.rs:30:29
|
LL | std::ptr::swap::<A>(std::ptr::null_mut(), &mut A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:29:37
--> tests/ui/invalid_null_ptr_usage.rs:31:37
|
LL | std::ptr::swap::<A>(&mut A, std::ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:31:44
--> tests/ui/invalid_null_ptr_usage.rs:33:44
|
LL | std::ptr::swap_nonoverlapping::<A>(std::ptr::null_mut(), &mut A, 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:32:52
--> tests/ui/invalid_null_ptr_usage.rs:34:52
|
LL | std::ptr::swap_nonoverlapping::<A>(&mut A, std::ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:34:25
--> tests/ui/invalid_null_ptr_usage.rs:36:25
|
LL | std::ptr::write(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:36:35
--> tests/ui/invalid_null_ptr_usage.rs:38:35
|
LL | std::ptr::write_unaligned(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:38:34
--> tests/ui/invalid_null_ptr_usage.rs:40:34
|
LL | std::ptr::write_volatile(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:40:40
--> tests/ui/invalid_null_ptr_usage.rs:42:40
|
LL | std::ptr::write_bytes::<usize>(std::ptr::null_mut(), 42, 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
Expand Down

0 comments on commit 31bbe2c

Please sign in to comment.