Skip to content

Commit 6e28b00

Browse files
committed
Address review comment: extend lint on std::slice::from_raw_parts_mut
1 parent 9083f00 commit 6e28b00

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

clippy_lints/src/ptr.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
173173
}
174174

175175
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
176+
const INVALID_NULL_PTR_USAGE_FNS: [&[&'static str]; 2] =
177+
[&paths::SLICE_FROM_RAW_PARTS, &paths::SLICE_FROM_RAW_PARTS_MUT];
178+
176179
if let ExprKind::Binary(ref op, ref l, ref r) = expr.kind {
177180
if (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne) && (is_null_path(l) || is_null_path(r)) {
178181
span_lint(
@@ -182,19 +185,22 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
182185
"comparing with null is better expressed by the `.is_null()` method",
183186
);
184187
}
185-
} else if let Some(args) = match_function_call(cx, expr, &paths::SLICE_FROM_RAW_PARTS) {
186-
if let Some(arg) = args.first() {
187-
if is_null_path(arg) {
188-
span_lint_and_sugg(
189-
cx,
190-
INVALID_NULL_PTR_USAGE,
191-
arg.span,
192-
"pointer must be non-null",
193-
"change this to",
194-
"core::ptr::NonNull::dangling().as_ptr()".to_string(),
195-
Applicability::MachineApplicable,
196-
);
197-
}
188+
} else if let Some(arg) = INVALID_NULL_PTR_USAGE_FNS
189+
.iter()
190+
.filter_map(|fn_name| match_function_call(cx, expr, fn_name))
191+
.next()
192+
.and_then(|args| args.first())
193+
{
194+
if is_null_path(arg) {
195+
span_lint_and_sugg(
196+
cx,
197+
INVALID_NULL_PTR_USAGE,
198+
arg.span,
199+
"pointer must be non-null",
200+
"change this to",
201+
"core::ptr::NonNull::dangling().as_ptr()".to_string(),
202+
Applicability::MachineApplicable,
203+
);
198204
}
199205
}
200206
}

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub const RWLOCK_WRITE_GUARD: [&str; 4] = ["std", "sync", "rwlock", "RwLockWrite
115115
pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"];
116116
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
117117
pub const SLICE_FROM_RAW_PARTS: [&str; 4] = ["core", "slice", "raw", "from_raw_parts"];
118+
pub const SLICE_FROM_RAW_PARTS_MUT: [&str; 4] = ["core", "slice", "raw", "from_raw_parts_mut"];
118119
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
119120
pub const SLICE_ITER: [&str; 4] = ["core", "slice", "iter", "Iter"];
120121
pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"];

tests/ui/invalid_null_ptr_usage.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ fn main() {
55
let _slice: &[usize] = unsafe { std::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0) };
66
let _slice: &[usize] = unsafe { std::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0) };
77
let _slice: &[usize] = unsafe { std::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0) };
8+
9+
let _slice: &[usize] = unsafe { std::slice::from_raw_parts_mut(core::ptr::NonNull::dangling().as_ptr(), 0) };
10+
let _slice: &[usize] = unsafe { std::slice::from_raw_parts_mut(core::ptr::NonNull::dangling().as_ptr(), 0) };
811
}

tests/ui/invalid_null_ptr_usage.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ fn main() {
55
let _slice: &[usize] = unsafe { std::slice::from_raw_parts(core::ptr::null(), 0) };
66
let _slice: &[usize] = unsafe { std::slice::from_raw_parts(std::ptr::null_mut(), 0) };
77
let _slice: &[usize] = unsafe { std::slice::from_raw_parts(core::ptr::null_mut(), 0) };
8+
9+
let _slice: &[usize] = unsafe { std::slice::from_raw_parts_mut(std::ptr::null_mut(), 0) };
10+
let _slice: &[usize] = unsafe { std::slice::from_raw_parts_mut(core::ptr::null_mut(), 0) };
811
}

tests/ui/invalid_null_ptr_usage.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,17 @@ error: pointer must be non-null
2424
LL | let _slice: &[usize] = unsafe { std::slice::from_raw_parts(core::ptr::null_mut(), 0) };
2525
| ^^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
2626

27-
error: aborting due to 4 previous errors
27+
error: pointer must be non-null
28+
--> $DIR/invalid_null_ptr_usage.rs:9:68
29+
|
30+
LL | let _slice: &[usize] = unsafe { std::slice::from_raw_parts_mut(std::ptr::null_mut(), 0) };
31+
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
32+
33+
error: pointer must be non-null
34+
--> $DIR/invalid_null_ptr_usage.rs:10:68
35+
|
36+
LL | let _slice: &[usize] = unsafe { std::slice::from_raw_parts_mut(core::ptr::null_mut(), 0) };
37+
| ^^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
38+
39+
error: aborting due to 6 previous errors
2840

0 commit comments

Comments
 (0)