@@ -260,18 +260,13 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
260260 ( None , kind) => {
261261 let expr_ty = typeck. expr_ty ( expr) ;
262262 let use_cx = expr_use_ctxt ( cx, expr) ;
263- let adjusted_ty = match & use_cx {
264- Some ( use_cx) => match use_cx. adjustments {
265- [ .., a] => a. target ,
266- _ => expr_ty,
267- } ,
268- _ => typeck. expr_ty_adjusted ( expr) ,
269- } ;
263+ let adjusted_ty = use_cx. adjustments . last ( ) . map_or ( expr_ty, |a| a. target ) ;
270264
271- match ( use_cx, kind) {
272- ( Some ( use_cx) , RefOp :: Deref ) => {
265+ match kind {
266+ RefOp :: Deref if use_cx. same_ctxt => {
267+ let use_node = use_cx. use_node ( cx) ;
273268 let sub_ty = typeck. expr_ty ( sub_expr) ;
274- if let ExprUseNode :: FieldAccess ( name) = use_cx . node
269+ if let ExprUseNode :: FieldAccess ( name) = use_node
275270 && !use_cx. moved_before_use
276271 && !ty_contains_field ( sub_ty, name. name )
277272 {
@@ -288,9 +283,9 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
288283 } else if sub_ty. is_ref ( )
289284 // Linting method receivers would require verifying that name lookup
290285 // would resolve the same way. This is complicated by trait methods.
291- && !use_cx . node . is_recv ( )
292- && let Some ( ty) = use_cx . node . defined_ty ( cx)
293- && TyCoercionStability :: for_defined_ty ( cx, ty, use_cx . node . is_return ( ) ) . is_deref_stable ( )
286+ && !use_node . is_recv ( )
287+ && let Some ( ty) = use_node . defined_ty ( cx)
288+ && TyCoercionStability :: for_defined_ty ( cx, ty, use_node . is_return ( ) ) . is_deref_stable ( )
294289 {
295290 self . state = Some ( (
296291 State :: ExplicitDeref { mutability : None } ,
@@ -301,7 +296,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
301296 ) ) ;
302297 }
303298 } ,
304- ( _ , RefOp :: Method { mutbl, is_ufcs } )
299+ RefOp :: Method { mutbl, is_ufcs }
305300 if !is_lint_allowed ( cx, EXPLICIT_DEREF_METHODS , expr. hir_id )
306301 // Allow explicit deref in method chains. e.g. `foo.deref().bar()`
307302 && ( is_ufcs || !in_postfix_position ( cx, expr) ) =>
@@ -319,7 +314,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
319314 } ,
320315 ) ) ;
321316 } ,
322- ( Some ( use_cx ) , RefOp :: AddrOf ( mutability) ) => {
317+ RefOp :: AddrOf ( mutability) if use_cx . same_ctxt => {
323318 // Find the number of times the borrow is auto-derefed.
324319 let mut iter = use_cx. adjustments . iter ( ) ;
325320 let mut deref_count = 0usize ;
@@ -338,10 +333,11 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
338333 } ;
339334 } ;
340335
341- let stability = use_cx. node . defined_ty ( cx) . map_or ( TyCoercionStability :: None , |ty| {
342- TyCoercionStability :: for_defined_ty ( cx, ty, use_cx. node . is_return ( ) )
336+ let use_node = use_cx. use_node ( cx) ;
337+ let stability = use_node. defined_ty ( cx) . map_or ( TyCoercionStability :: None , |ty| {
338+ TyCoercionStability :: for_defined_ty ( cx, ty, use_node. is_return ( ) )
343339 } ) ;
344- let can_auto_borrow = match use_cx . node {
340+ let can_auto_borrow = match use_node {
345341 ExprUseNode :: FieldAccess ( _)
346342 if !use_cx. moved_before_use && matches ! ( sub_expr. kind, ExprKind :: Field ( ..) ) =>
347343 {
@@ -353,7 +349,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
353349 // deref through `ManuallyDrop<_>` will not compile.
354350 !adjust_derefs_manually_drop ( use_cx. adjustments , expr_ty)
355351 } ,
356- ExprUseNode :: Callee | ExprUseNode :: FieldAccess ( _) => true ,
352+ ExprUseNode :: Callee | ExprUseNode :: FieldAccess ( _) if !use_cx . moved_before_use => true ,
357353 ExprUseNode :: MethodArg ( hir_id, _, 0 ) if !use_cx. moved_before_use => {
358354 // Check for calls to trait methods where the trait is implemented
359355 // on a reference.
@@ -363,9 +359,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
363359 // priority.
364360 if let Some ( fn_id) = typeck. type_dependent_def_id ( hir_id)
365361 && let Some ( trait_id) = cx. tcx . trait_of_item ( fn_id)
366- && let arg_ty = cx
367- . tcx
368- . erase_regions ( use_cx. adjustments . last ( ) . map_or ( expr_ty, |a| a. target ) )
362+ && let arg_ty = cx. tcx . erase_regions ( adjusted_ty)
369363 && let ty:: Ref ( _, sub_ty, _) = * arg_ty. kind ( )
370364 && let args =
371365 typeck. node_args_opt ( hir_id) . map ( |args| & args[ 1 ..] ) . unwrap_or_default ( )
@@ -443,7 +437,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
443437 count : deref_count - required_refs,
444438 msg,
445439 stability,
446- for_field_access : if let ExprUseNode :: FieldAccess ( name) = use_cx . node
440+ for_field_access : if let ExprUseNode :: FieldAccess ( name) = use_node
447441 && !use_cx. moved_before_use
448442 {
449443 Some ( name. name )
@@ -453,7 +447,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
453447 } ) ,
454448 StateData {
455449 first_expr : expr,
456- adjusted_ty : use_cx . adjustments . last ( ) . map_or ( expr_ty , |a| a . target ) ,
450+ adjusted_ty,
457451 } ,
458452 ) ) ;
459453 } else if stability. is_deref_stable ( )
@@ -465,12 +459,12 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
465459 State :: Borrow { mutability } ,
466460 StateData {
467461 first_expr : expr,
468- adjusted_ty : use_cx . adjustments . last ( ) . map_or ( expr_ty , |a| a . target ) ,
462+ adjusted_ty,
469463 } ,
470464 ) ) ;
471465 }
472466 } ,
473- ( None , _ ) | ( _ , RefOp :: Method { .. } ) => ( ) ,
467+ _ => { } ,
474468 }
475469 } ,
476470 (
0 commit comments