@@ -58,25 +58,6 @@ impl LateLintPass<'_> for DefaultHashTypes {
5858 }
5959}
6060
61- /// Helper function for lints that check for expressions with calls and use typeck results to
62- /// get the `DefId` and `GenericArgsRef` of the function.
63- fn typeck_results_of_method_fn < ' tcx > (
64- cx : & LateContext < ' tcx > ,
65- expr : & hir:: Expr < ' _ > ,
66- ) -> Option < ( Span , DefId , ty:: GenericArgsRef < ' tcx > ) > {
67- match expr. kind {
68- hir:: ExprKind :: MethodCall ( segment, ..)
69- if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) =>
70- {
71- Some ( ( segment. ident . span , def_id, cx. typeck_results ( ) . node_args ( expr. hir_id ) ) )
72- }
73- _ => match cx. typeck_results ( ) . node_type ( expr. hir_id ) . kind ( ) {
74- & ty:: FnDef ( def_id, args) => Some ( ( expr. span , def_id, args) ) ,
75- _ => None ,
76- } ,
77- }
78- }
79-
8061declare_tool_lint ! {
8162 /// The `potential_query_instability` lint detects use of methods which can lead to
8263 /// potential query instability, such as iterating over a `HashMap`.
@@ -105,9 +86,10 @@ declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY, UNTRACKED_QUE
10586
10687impl < ' tcx > LateLintPass < ' tcx > for QueryStability {
10788 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
108- if let Some ( ( span, def_id, args) ) = typeck_results_of_method_fn ( cx, expr)
89+ if let Some ( ( def_id, span, generic_args, _recv, _args) ) =
90+ get_callee_span_generic_args_and_args ( cx, expr)
10991 && let Ok ( Some ( instance) ) =
110- ty:: Instance :: try_resolve ( cx. tcx , cx. typing_env ( ) , def_id, args )
92+ ty:: Instance :: try_resolve ( cx. tcx , cx. typing_env ( ) , def_id, generic_args )
11193 {
11294 let def_id = instance. def_id ( ) ;
11395 if cx. tcx . has_attr ( def_id, sym:: rustc_lint_query_instability) {
@@ -137,8 +119,8 @@ fn check_into_iter_stability<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx
137119 return ;
138120 } ;
139121 // Is `expr` a function or method call?
140- let Some ( ( callee_def_id, generic_args, recv, args) ) =
141- get_callee_generic_args_and_args ( cx, expr)
122+ let Some ( ( callee_def_id, _span , generic_args, recv, args) ) =
123+ get_callee_span_generic_args_and_args ( cx, expr)
142124 else {
143125 return ;
144126 } ;
@@ -195,23 +177,23 @@ fn check_into_iter_stability<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx
195177}
196178
197179/// Checks whether an expression is a function or method call and, if so, returns its `DefId`,
198- /// `GenericArgs`, and arguments.
199- fn get_callee_generic_args_and_args < ' tcx > (
180+ /// `Span`, `GenericArgs`, and arguments. This is a slight augmentation of a similarly named Clippy
181+ /// function, `get_callee_generic_args_and_args`.
182+ fn get_callee_span_generic_args_and_args < ' tcx > (
200183 cx : & LateContext < ' tcx > ,
201184 expr : & ' tcx Expr < ' tcx > ,
202- ) -> Option < ( DefId , GenericArgsRef < ' tcx > , Option < & ' tcx Expr < ' tcx > > , & ' tcx [ Expr < ' tcx > ] ) > {
185+ ) -> Option < ( DefId , Span , GenericArgsRef < ' tcx > , Option < & ' tcx Expr < ' tcx > > , & ' tcx [ Expr < ' tcx > ] ) > {
203186 if let ExprKind :: Call ( callee, args) = expr. kind
204- && let callee_ty = cx. typeck_results ( ) . expr_ty ( callee)
205- && let ty:: FnDef ( callee_def_id, _ ) = callee_ty. kind ( )
187+ && let callee_ty = cx. typeck_results ( ) . expr_ty_adjusted ( callee)
188+ && let ty:: FnDef ( callee_def_id, generic_args ) = callee_ty. kind ( )
206189 {
207- let generic_args = cx. typeck_results ( ) . node_args ( callee. hir_id ) ;
208- return Some ( ( * callee_def_id, generic_args, None , args) ) ;
190+ return Some ( ( * callee_def_id, callee. span , generic_args, None , args) ) ;
209191 }
210- if let ExprKind :: MethodCall ( _ , recv, args, _) = expr. kind
192+ if let ExprKind :: MethodCall ( segment , recv, args, _) = expr. kind
211193 && let Some ( method_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id )
212194 {
213195 let generic_args = cx. typeck_results ( ) . node_args ( expr. hir_id ) ;
214- return Some ( ( method_def_id, generic_args, Some ( recv) , args) ) ;
196+ return Some ( ( method_def_id, segment . ident . span , generic_args, Some ( recv) , args) ) ;
215197 }
216198 None
217199}
@@ -552,33 +534,22 @@ declare_tool_lint! {
552534declare_lint_pass ! ( Diagnostics => [ UNTRANSLATABLE_DIAGNOSTIC , DIAGNOSTIC_OUTSIDE_OF_IMPL ] ) ;
553535
554536impl LateLintPass < ' _ > for Diagnostics {
555- fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) {
537+ fn check_expr < ' tcx > ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' tcx > ) {
556538 let collect_args_tys_and_spans = |args : & [ hir:: Expr < ' _ > ] , reserve_one_extra : bool | {
557539 let mut result = Vec :: with_capacity ( args. len ( ) + usize:: from ( reserve_one_extra) ) ;
558540 result. extend ( args. iter ( ) . map ( |arg| ( cx. typeck_results ( ) . expr_ty ( arg) , arg. span ) ) ) ;
559541 result
560542 } ;
561543 // Only check function calls and method calls.
562- let ( span, def_id, fn_gen_args, arg_tys_and_spans) = match expr. kind {
563- hir:: ExprKind :: Call ( callee, args) => {
564- match cx. typeck_results ( ) . node_type ( callee. hir_id ) . kind ( ) {
565- & ty:: FnDef ( def_id, fn_gen_args) => {
566- ( callee. span , def_id, fn_gen_args, collect_args_tys_and_spans ( args, false ) )
567- }
568- _ => return , // occurs for fns passed as args
569- }
570- }
571- hir:: ExprKind :: MethodCall ( _segment, _recv, args, _span) => {
572- let Some ( ( span, def_id, fn_gen_args) ) = typeck_results_of_method_fn ( cx, expr)
573- else {
574- return ;
575- } ;
576- let mut args = collect_args_tys_and_spans ( args, true ) ;
577- args. insert ( 0 , ( cx. tcx . types . self_param , _recv. span ) ) ; // dummy inserted for `self`
578- ( span, def_id, fn_gen_args, args)
579- }
580- _ => return ,
544+ let Some ( ( def_id, span, fn_gen_args, recv, args) ) =
545+ get_callee_span_generic_args_and_args ( cx, expr)
546+ else {
547+ return ;
581548 } ;
549+ let mut arg_tys_and_spans = collect_args_tys_and_spans ( args, recv. is_some ( ) ) ;
550+ if let Some ( recv) = recv {
551+ arg_tys_and_spans. insert ( 0 , ( cx. tcx . types . self_param , recv. span ) ) ; // dummy inserted for `self`
552+ }
582553
583554 Self :: diagnostic_outside_of_impl ( cx, span, expr. hir_id , def_id, fn_gen_args) ;
584555 Self :: untranslatable_diagnostic ( cx, def_id, & arg_tys_and_spans) ;
0 commit comments