@@ -380,61 +380,46 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
380380 let typeck = self . infcx . tcx . typeck ( self . mir_def_id ( ) ) ;
381381 let hir_id = hir. get_parent_node ( expr. hir_id ) ;
382382 if let Some ( parent) = hir. find ( hir_id) {
383- if let hir:: Node :: Expr ( parent_expr) = parent
383+ let ( def_id , args , offset ) = if let hir:: Node :: Expr ( parent_expr) = parent
384384 && let hir:: ExprKind :: MethodCall ( _, _, args, _) = parent_expr. kind
385385 && let Some ( def_id) = typeck. type_dependent_def_id ( parent_expr. hir_id )
386- && let Some ( def_id) = def_id. as_local ( )
387- && let Some ( node) = hir. find ( hir. local_def_id_to_hir_id ( def_id) )
388- && let Some ( fn_sig) = node. fn_sig ( )
389- && let Some ( ident) = node. ident ( )
390- && let Some ( pos) = args. iter ( )
391- . position ( |arg| arg. hir_id == expr. hir_id )
392- && let Some ( arg) = fn_sig. decl . inputs . get ( pos + 1 )
393386 {
394- let mut span: MultiSpan = arg. span . into ( ) ;
395- span. push_span_label (
396- arg. span ,
397- "this type parameter takes ownership of the value" . to_string ( ) ,
398- ) ;
399- span. push_span_label (
400- ident. span ,
401- "in this method" . to_string ( ) ,
402- ) ;
403- err. span_note (
404- span,
405- format ! (
406- "consider changing this parameter type in `{}` to borrow instead \
407- if ownering the value isn't necessary",
408- ident,
409- ) ,
410- ) ;
411- }
412- if let hir:: Node :: Expr ( parent_expr) = parent
387+ ( def_id. as_local ( ) , args, 1 )
388+ } else if let hir:: Node :: Expr ( parent_expr) = parent
413389 && let hir:: ExprKind :: Call ( call, args) = parent_expr. kind
414390 && let ty:: FnDef ( def_id, _) = typeck. node_type ( call. hir_id ) . kind ( )
415- && let Some ( def_id) = def_id. as_local ( )
391+ {
392+ ( def_id. as_local ( ) , args, 0 )
393+ } else {
394+ ( None , & [ ] [ ..] , 0 )
395+ } ;
396+ if let Some ( def_id) = def_id
416397 && let Some ( node) = hir. find ( hir. local_def_id_to_hir_id ( def_id) )
417398 && let Some ( fn_sig) = node. fn_sig ( )
418399 && let Some ( ident) = node. ident ( )
419400 && let Some ( pos) = args. iter ( )
420401 . position ( |arg| arg. hir_id == expr. hir_id )
421- && let Some ( arg) = fn_sig. decl . inputs . get ( pos)
402+ && let Some ( arg) = fn_sig. decl . inputs . get ( pos + offset )
422403 {
423404 let mut span: MultiSpan = arg. span . into ( ) ;
424405 span. push_span_label (
425406 arg. span ,
426407 "this type parameter takes ownership of the value" . to_string ( ) ,
427408 ) ;
409+ let descr = match node. fn_kind ( ) {
410+ Some ( hir:: intravisit:: FnKind :: ItemFn ( ..) ) | None => "function" ,
411+ Some ( hir:: intravisit:: FnKind :: Method ( ..) ) => "method" ,
412+ Some ( hir:: intravisit:: FnKind :: Closure ) => "closure" ,
413+ } ;
428414 span. push_span_label (
429415 ident. span ,
430- "in this function" . to_string ( ) ,
416+ format ! ( "in this {descr}" ) ,
431417 ) ;
432418 err. span_note (
433419 span,
434420 format ! (
435- "consider changing this parameter type in `{}` to borrow instead \
436- if ownering the value isn't necessary",
437- ident,
421+ "consider changing this parameter type in {descr} `{ident}` to \
422+ borrow instead if ownering the value isn't necessary",
438423 ) ,
439424 ) ;
440425 }
0 commit comments