From e1ec41b2179e89a657bae77c6a4ed15da65ad040 Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Thu, 24 Aug 2023 21:06:18 +0200 Subject: [PATCH] Fix dogfood issues --- clippy_lints/src/casts/cast_ptr_alignment.rs | 6 +++-- clippy_lints/src/ptr.rs | 25 +++++++++++++++----- clippy_utils/src/paths.rs | 3 --- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/casts/cast_ptr_alignment.rs b/clippy_lints/src/casts/cast_ptr_alignment.rs index 5bf467efa..1de691221 100644 --- a/clippy_lints/src/casts/cast_ptr_alignment.rs +++ b/clippy_lints/src/casts/cast_ptr_alignment.rs @@ -5,6 +5,7 @@ use rustc_hir::{Expr, ExprKind, GenericArg}; use rustc_lint::LateContext; use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::{self, Ty}; +use rustc_span::sym; use super::CAST_PTR_ALIGNMENT; @@ -76,13 +77,14 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool { ExprKind::Call(func, [arg, ..]) if arg.hir_id == e.hir_id => { static PATHS: &[&[&str]] = &[ paths::PTR_READ_UNALIGNED.as_slice(), - paths::PTR_WRITE_UNALIGNED.as_slice(), paths::PTR_UNALIGNED_VOLATILE_LOAD.as_slice(), paths::PTR_UNALIGNED_VOLATILE_STORE.as_slice(), ]; + if let ExprKind::Path(path) = &func.kind && let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id() - && match_any_def_paths(cx, def_id, PATHS).is_some() + && (match_any_def_paths(cx, def_id, PATHS).is_some() + || cx.tcx.is_diagnostic_item(sym::ptr_write_unaligned, def_id)) { true } else { diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 8009b00b4..bf031ac84 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -278,7 +278,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { // (fn_path, arg_indices) - `arg_indices` are the `arg` positions where null would cause U.B. - const INVALID_NULL_PTR_USAGE_TABLE: [(&[&str], &[usize]); 16] = [ + const INVALID_NULL_PTR_USAGE_TABLE: [(&[&str], &[usize]); 13] = [ (&paths::SLICE_FROM_RAW_PARTS, &[0]), (&paths::SLICE_FROM_RAW_PARTS_MUT, &[0]), (&paths::PTR_COPY, &[0, 1]), @@ -291,20 +291,33 @@ fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { (&paths::PTR_SLICE_FROM_RAW_PARTS_MUT, &[0]), (&paths::PTR_SWAP, &[0, 1]), (&paths::PTR_SWAP_NONOVERLAPPING, &[0, 1]), - (&paths::PTR_WRITE, &[0]), - (&paths::PTR_WRITE_UNALIGNED, &[0]), - (&paths::PTR_WRITE_VOLATILE, &[0]), (&paths::PTR_WRITE_BYTES, &[0]), ]; + let invalid_null_ptr_usage_table_diag_items: [(Option, &[usize]); 3] = [ + (cx.tcx.get_diagnostic_item(sym::ptr_write), &[0]), + (cx.tcx.get_diagnostic_item(sym::ptr_write_unaligned), &[0]), + (cx.tcx.get_diagnostic_item(sym::ptr_write_volatile), &[0]), + ]; if_chain! { if let ExprKind::Call(fun, args) = expr.kind; if let ExprKind::Path(ref qpath) = fun.kind; if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id(); let fun_def_path = cx.get_def_path(fun_def_id).into_iter().map(Symbol::to_ident_string).collect::>(); - if let Some(&(_, arg_indices)) = INVALID_NULL_PTR_USAGE_TABLE + if let Some(arg_indices) = INVALID_NULL_PTR_USAGE_TABLE .iter() - .find(|&&(fn_path, _)| fn_path == fun_def_path); + .find_map(|&(fn_path, indices)| if fn_path == fun_def_path { Some(indices) } else { None }) + .or_else(|| { + invalid_null_ptr_usage_table_diag_items + .iter() + .find_map(|&(def_id, indices)| { + if def_id == Some(fun_def_id) { + Some(indices) + } else { + None + } + }) + }); then { for &arg_idx in arg_indices { if let Some(arg) = args.get(arg_idx).filter(|arg| is_null_path(cx, arg)) { diff --git a/clippy_utils/src/paths.rs b/clippy_utils/src/paths.rs index e72d063cf..a567c5cbd 100644 --- a/clippy_utils/src/paths.rs +++ b/clippy_utils/src/paths.rs @@ -87,10 +87,7 @@ pub const PTR_REPLACE: [&str; 3] = ["core", "ptr", "replace"]; pub const PTR_SWAP: [&str; 3] = ["core", "ptr", "swap"]; pub const PTR_UNALIGNED_VOLATILE_LOAD: [&str; 3] = ["core", "intrinsics", "unaligned_volatile_load"]; pub const PTR_UNALIGNED_VOLATILE_STORE: [&str; 3] = ["core", "intrinsics", "unaligned_volatile_store"]; -pub const PTR_WRITE: [&str; 3] = ["core", "ptr", "write"]; pub const PTR_WRITE_BYTES: [&str; 3] = ["core", "intrinsics", "write_bytes"]; -pub const PTR_WRITE_UNALIGNED: [&str; 3] = ["core", "ptr", "write_unaligned"]; -pub const PTR_WRITE_VOLATILE: [&str; 3] = ["core", "ptr", "write_volatile"]; pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"]; pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"]; pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];