Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e1ec41b

Browse files
committed
Fix dogfood issues
1 parent ef4d64f commit e1ec41b

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir::{Expr, ExprKind, GenericArg};
55
use rustc_lint::LateContext;
66
use rustc_middle::ty::layout::LayoutOf;
77
use rustc_middle::ty::{self, Ty};
8+
use rustc_span::sym;
89

910
use super::CAST_PTR_ALIGNMENT;
1011

@@ -76,13 +77,14 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
7677
ExprKind::Call(func, [arg, ..]) if arg.hir_id == e.hir_id => {
7778
static PATHS: &[&[&str]] = &[
7879
paths::PTR_READ_UNALIGNED.as_slice(),
79-
paths::PTR_WRITE_UNALIGNED.as_slice(),
8080
paths::PTR_UNALIGNED_VOLATILE_LOAD.as_slice(),
8181
paths::PTR_UNALIGNED_VOLATILE_STORE.as_slice(),
8282
];
83+
8384
if let ExprKind::Path(path) = &func.kind
8485
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
85-
&& match_any_def_paths(cx, def_id, PATHS).is_some()
86+
&& (match_any_def_paths(cx, def_id, PATHS).is_some()
87+
|| cx.tcx.is_diagnostic_item(sym::ptr_write_unaligned, def_id))
8688
{
8789
true
8890
} else {

clippy_lints/src/ptr.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
278278

279279
fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
280280
// (fn_path, arg_indices) - `arg_indices` are the `arg` positions where null would cause U.B.
281-
const INVALID_NULL_PTR_USAGE_TABLE: [(&[&str], &[usize]); 16] = [
281+
const INVALID_NULL_PTR_USAGE_TABLE: [(&[&str], &[usize]); 13] = [
282282
(&paths::SLICE_FROM_RAW_PARTS, &[0]),
283283
(&paths::SLICE_FROM_RAW_PARTS_MUT, &[0]),
284284
(&paths::PTR_COPY, &[0, 1]),
@@ -291,20 +291,33 @@ fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
291291
(&paths::PTR_SLICE_FROM_RAW_PARTS_MUT, &[0]),
292292
(&paths::PTR_SWAP, &[0, 1]),
293293
(&paths::PTR_SWAP_NONOVERLAPPING, &[0, 1]),
294-
(&paths::PTR_WRITE, &[0]),
295-
(&paths::PTR_WRITE_UNALIGNED, &[0]),
296-
(&paths::PTR_WRITE_VOLATILE, &[0]),
297294
(&paths::PTR_WRITE_BYTES, &[0]),
298295
];
296+
let invalid_null_ptr_usage_table_diag_items: [(Option<DefId>, &[usize]); 3] = [
297+
(cx.tcx.get_diagnostic_item(sym::ptr_write), &[0]),
298+
(cx.tcx.get_diagnostic_item(sym::ptr_write_unaligned), &[0]),
299+
(cx.tcx.get_diagnostic_item(sym::ptr_write_volatile), &[0]),
300+
];
299301

300302
if_chain! {
301303
if let ExprKind::Call(fun, args) = expr.kind;
302304
if let ExprKind::Path(ref qpath) = fun.kind;
303305
if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
304306
let fun_def_path = cx.get_def_path(fun_def_id).into_iter().map(Symbol::to_ident_string).collect::<Vec<_>>();
305-
if let Some(&(_, arg_indices)) = INVALID_NULL_PTR_USAGE_TABLE
307+
if let Some(arg_indices) = INVALID_NULL_PTR_USAGE_TABLE
306308
.iter()
307-
.find(|&&(fn_path, _)| fn_path == fun_def_path);
309+
.find_map(|&(fn_path, indices)| if fn_path == fun_def_path { Some(indices) } else { None })
310+
.or_else(|| {
311+
invalid_null_ptr_usage_table_diag_items
312+
.iter()
313+
.find_map(|&(def_id, indices)| {
314+
if def_id == Some(fun_def_id) {
315+
Some(indices)
316+
} else {
317+
None
318+
}
319+
})
320+
});
308321
then {
309322
for &arg_idx in arg_indices {
310323
if let Some(arg) = args.get(arg_idx).filter(|arg| is_null_path(cx, arg)) {

clippy_utils/src/paths.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ pub const PTR_REPLACE: [&str; 3] = ["core", "ptr", "replace"];
8787
pub const PTR_SWAP: [&str; 3] = ["core", "ptr", "swap"];
8888
pub const PTR_UNALIGNED_VOLATILE_LOAD: [&str; 3] = ["core", "intrinsics", "unaligned_volatile_load"];
8989
pub const PTR_UNALIGNED_VOLATILE_STORE: [&str; 3] = ["core", "intrinsics", "unaligned_volatile_store"];
90-
pub const PTR_WRITE: [&str; 3] = ["core", "ptr", "write"];
9190
pub const PTR_WRITE_BYTES: [&str; 3] = ["core", "intrinsics", "write_bytes"];
92-
pub const PTR_WRITE_UNALIGNED: [&str; 3] = ["core", "ptr", "write_unaligned"];
93-
pub const PTR_WRITE_VOLATILE: [&str; 3] = ["core", "ptr", "write_volatile"];
9491
pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
9592
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
9693
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];

0 commit comments

Comments
 (0)