Skip to content

Commit 31bbe2c

Browse files
committed
Apply review suggestions
1 parent 11363c7 commit 31bbe2c

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

clippy_lints/src/ptr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
271271
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
272272
&& let Some(name) = cx.tcx.get_diagnostic_name(fun_def_id)
273273
{
274+
// TODO: `ptr_slice_from_raw_parts` and its mutable variant should probably still be linted
275+
// conditionally based on how the return value is used, but not universally like the other
276+
// functions since there are valid uses for null slice pointers.
277+
//
278+
// See: https://github.com/rust-lang/rust-clippy/pull/13452/files#r1773772034
279+
274280
// `arg` positions where null would cause U.B.
275281
let arg_indices: &[_] = match name {
276282
sym::ptr_read

tests/ui/invalid_null_ptr_usage.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ fn main() {
2424
let _a: A = std::ptr::read_volatile(core::ptr::NonNull::dangling().as_ptr());
2525

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

2830
std::ptr::swap::<A>(core::ptr::NonNull::dangling().as_ptr(), &mut A);
2931
std::ptr::swap::<A>(&mut A, core::ptr::NonNull::dangling().as_ptr());

tests/ui/invalid_null_ptr_usage.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ fn main() {
2424
let _a: A = std::ptr::read_volatile(std::ptr::null_mut());
2525

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

2830
std::ptr::swap::<A>(std::ptr::null_mut(), &mut A);
2931
std::ptr::swap::<A>(&mut A, std::ptr::null_mut());

tests/ui/invalid_null_ptr_usage.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,49 +85,49 @@ LL | let _a: A = std::ptr::replace(std::ptr::null_mut(), A);
8585
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
8686

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)