22#![ deny( clippy:: missing_docs_in_private_items) ]
33
44use crate :: source:: { snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite} ;
5+ use crate :: ty:: expr_sig;
56use crate :: { get_parent_expr_for_hir, higher} ;
67use rustc_ast:: util:: parser:: AssocOp ;
78use rustc_ast:: { ast, token} ;
@@ -18,7 +19,6 @@ use rustc_span::source_map::{BytePos, CharPos, Pos, Span, SyntaxContext};
1819use rustc_typeck:: expr_use_visitor:: { Delegate , ExprUseVisitor , PlaceBase , PlaceWithHirId } ;
1920use std:: borrow:: Cow ;
2021use std:: fmt:: { Display , Write as _} ;
21- use std:: iter;
2222use std:: ops:: { Add , Neg , Not , Sub } ;
2323
2424/// A helper type to build suggestion correctly handling parentheses.
@@ -861,23 +861,37 @@ impl<'tcx> DerefDelegate<'_, 'tcx> {
861861
862862 /// indicates whether the function from `parent_expr` takes its args by double reference
863863 fn func_takes_arg_by_double_ref ( & self , parent_expr : & ' tcx hir:: Expr < ' _ > , cmt_hir_id : HirId ) -> bool {
864- let ( call_args , inputs ) = match parent_expr. kind {
864+ let ty = match parent_expr. kind {
865865 ExprKind :: MethodCall ( _, call_args, _) => {
866- if let Some ( method_did) = self . cx . typeck_results ( ) . type_dependent_def_id ( parent_expr. hir_id ) {
867- ( call_args, self . cx . tcx . fn_sig ( method_did) . skip_binder ( ) . inputs ( ) )
866+ if let Some ( sig) = self
867+ . cx
868+ . typeck_results ( )
869+ . type_dependent_def_id ( parent_expr. hir_id )
870+ . map ( |did| self . cx . tcx . fn_sig ( did) . skip_binder ( ) )
871+ {
872+ call_args
873+ . iter ( )
874+ . position ( |arg| arg. hir_id == cmt_hir_id)
875+ . map ( |i| sig. inputs ( ) [ i] )
868876 } else {
869877 return false ;
870878 }
871879 } ,
872880 ExprKind :: Call ( func, call_args) => {
873- let typ = self . cx . typeck_results ( ) . expr_ty ( func) ;
874- ( call_args, typ. fn_sig ( self . cx . tcx ) . skip_binder ( ) . inputs ( ) )
881+ if let Some ( sig) = expr_sig ( self . cx , func) {
882+ call_args
883+ . iter ( )
884+ . position ( |arg| arg. hir_id == cmt_hir_id)
885+ . and_then ( |i| sig. input ( i) )
886+ . map ( ty:: Binder :: skip_binder)
887+ } else {
888+ return false ;
889+ }
875890 } ,
876891 _ => return false ,
877892 } ;
878893
879- iter:: zip ( call_args, inputs)
880- . any ( |( arg, ty) | arg. hir_id == cmt_hir_id && matches ! ( ty. kind( ) , ty:: Ref ( _, inner, _) if inner. is_ref( ) ) )
894+ ty. map_or ( false , |ty| matches ! ( ty. kind( ) , ty:: Ref ( _, inner, _) if inner. is_ref( ) ) )
881895 }
882896}
883897
0 commit comments