@@ -278,7 +278,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
278
278
279
279
fn check_invalid_ptr_usage < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
280
280
// (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 ] = [
282
282
( & paths:: SLICE_FROM_RAW_PARTS , & [ 0 ] ) ,
283
283
( & paths:: SLICE_FROM_RAW_PARTS_MUT , & [ 0 ] ) ,
284
284
( & paths:: PTR_COPY , & [ 0 , 1 ] ) ,
@@ -291,20 +291,33 @@ fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
291
291
( & paths:: PTR_SLICE_FROM_RAW_PARTS_MUT , & [ 0 ] ) ,
292
292
( & paths:: PTR_SWAP , & [ 0 , 1 ] ) ,
293
293
( & paths:: PTR_SWAP_NONOVERLAPPING , & [ 0 , 1 ] ) ,
294
- ( & paths:: PTR_WRITE , & [ 0 ] ) ,
295
- ( & paths:: PTR_WRITE_UNALIGNED , & [ 0 ] ) ,
296
- ( & paths:: PTR_WRITE_VOLATILE , & [ 0 ] ) ,
297
294
( & paths:: PTR_WRITE_BYTES , & [ 0 ] ) ,
298
295
] ;
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
+ ] ;
299
301
300
302
if_chain ! {
301
303
if let ExprKind :: Call ( fun, args) = expr. kind;
302
304
if let ExprKind :: Path ( ref qpath) = fun. kind;
303
305
if let Some ( fun_def_id) = cx. qpath_res( qpath, fun. hir_id) . opt_def_id( ) ;
304
306
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
306
308
. 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
+ } ) ;
308
321
then {
309
322
for & arg_idx in arg_indices {
310
323
if let Some ( arg) = args. get( arg_idx) . filter( |arg| is_null_path( cx, arg) ) {
0 commit comments