@@ -237,7 +237,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
237
237
. collect ( ) ;
238
238
let results = check_ptr_arg_usage ( cx, body, & lint_args) ;
239
239
240
- for ( result, args) in results . iter ( ) . zip ( lint_args. iter ( ) ) . filter ( |( r, _) | !r. skip ) {
240
+ for ( result, args) in iter:: zip ( & results , & lint_args) . filter ( |( r, _) | !r. skip ) {
241
241
span_lint_hir_and_then ( cx, PTR_ARG , args. emission_id , args. span , args. build_msg ( ) , |diag| {
242
242
diag. multipart_suggestion (
243
243
"change this to" ,
@@ -386,7 +386,6 @@ impl<'tcx> DerefTy<'tcx> {
386
386
}
387
387
}
388
388
389
- #[ expect( clippy:: too_many_lines) ]
390
389
fn check_fn_args < ' cx , ' tcx : ' cx > (
391
390
cx : & ' cx LateContext < ' tcx > ,
392
391
fn_sig : ty:: FnSig < ' tcx > ,
@@ -413,13 +412,13 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
413
412
Some ( sym:: Vec ) => (
414
413
[ ( sym:: clone, ".to_owned()" ) ] . as_slice ( ) ,
415
414
DerefTy :: Slice (
416
- name . args . and_then ( |args| args . args . first ( ) ) . and_then ( |arg| {
417
- if let GenericArg :: Type ( ty) = arg {
418
- Some ( ty . span )
419
- } else {
420
- None
421
- }
422
- } ) ,
415
+ if let Some ( name_args ) = name . args
416
+ && let [ GenericArg :: Type ( ty) , .. ] = name_args . args
417
+ {
418
+ Some ( ty . span )
419
+ } else {
420
+ None
421
+ } ,
423
422
args. type_at ( 0 ) ,
424
423
) ,
425
424
) ,
@@ -432,33 +431,29 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
432
431
DerefTy :: Path ,
433
432
) ,
434
433
Some ( sym:: Cow ) if mutability == Mutability :: Not => {
435
- if let Some ( ( lifetime, ty) ) = name. args . and_then ( |args| {
436
- if let [ GenericArg :: Lifetime ( lifetime) , ty] = args. args {
437
- return Some ( ( lifetime, ty) ) ;
438
- }
439
- None
440
- } ) {
434
+ if let Some ( name_args) = name. args
435
+ && let [ GenericArg :: Lifetime ( lifetime) , ty] = name_args. args
436
+ {
441
437
if let LifetimeKind :: Param ( param_def_id) = lifetime. kind
442
438
&& !lifetime. is_anonymous ( )
443
439
&& fn_sig
444
440
. output ( )
445
441
. walk ( )
446
- . filter_map ( |arg| {
447
- arg. as_region ( ) . and_then ( |lifetime| match lifetime. kind ( ) {
448
- ty:: ReEarlyParam ( r) => Some (
449
- cx. tcx
450
- . generics_of ( cx. tcx . parent ( param_def_id. to_def_id ( ) ) )
451
- . region_param ( r, cx. tcx )
452
- . def_id ,
453
- ) ,
454
- ty:: ReBound ( _, r) => r. kind . get_id ( ) ,
455
- ty:: ReLateParam ( r) => r. kind . get_id ( ) ,
456
- ty:: ReStatic
457
- | ty:: ReVar ( _)
458
- | ty:: RePlaceholder ( _)
459
- | ty:: ReErased
460
- | ty:: ReError ( _) => None ,
461
- } )
442
+ . filter_map ( ty:: GenericArg :: as_region)
443
+ . filter_map ( |lifetime| match lifetime. kind ( ) {
444
+ ty:: ReEarlyParam ( r) => Some (
445
+ cx. tcx
446
+ . generics_of ( cx. tcx . parent ( param_def_id. to_def_id ( ) ) )
447
+ . region_param ( r, cx. tcx )
448
+ . def_id ,
449
+ ) ,
450
+ ty:: ReBound ( _, r) => r. kind . get_id ( ) ,
451
+ ty:: ReLateParam ( r) => r. kind . get_id ( ) ,
452
+ ty:: ReStatic
453
+ | ty:: ReVar ( _)
454
+ | ty:: RePlaceholder ( _)
455
+ | ty:: ReErased
456
+ | ty:: ReError ( _) => None ,
462
457
} )
463
458
. any ( |def_id| def_id. as_local ( ) . is_some_and ( |def_id| def_id == param_def_id) )
464
459
{
@@ -627,12 +622,16 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, args: &[
627
622
}
628
623
}
629
624
625
+ // If the expression's type gets adjusted down to the deref type, we might as
626
+ // well have started with that deref type -- the lint should fire
630
627
let deref_ty = args. deref_ty . ty ( self . cx ) ;
631
628
let adjusted_ty = self . cx . typeck_results ( ) . expr_ty_adjusted ( e) . peel_refs ( ) ;
632
629
if adjusted_ty == deref_ty {
633
630
return ;
634
631
}
635
632
633
+ // If the expression's type is constrained by `dyn Trait`, see if the deref
634
+ // type implements the trait(s) as well, and if so, the lint should fire
636
635
if let ty:: Dynamic ( preds, ..) = adjusted_ty. kind ( )
637
636
&& matches_preds ( self . cx , deref_ty, preds)
638
637
{
0 commit comments