@@ -278,7 +278,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
278278
279279fn 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) ) {
0 commit comments