11use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_hir_and_then} ;
22use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
33use clippy_utils:: sugg:: has_enclosing_paren;
4- use clippy_utils:: ty:: { expr_sig, peel_mid_ty_refs, variant_of_res} ;
4+ use clippy_utils:: ty:: { expr_sig, peel_mid_ty_refs, ty_sig , variant_of_res} ;
55use clippy_utils:: { get_parent_expr, is_lint_allowed, path_to_local, walk_to_expr_usage} ;
66use rustc_ast:: util:: parser:: { PREC_POSTFIX , PREC_PREFIX } ;
77use rustc_data_structures:: fx:: FxIndexMap ;
@@ -704,24 +704,13 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
704704 let owner_id = cx. tcx . hir ( ) . body_owner ( cx. enclosing_body . unwrap ( ) ) ;
705705 Some (
706706 if let Node :: Expr (
707- closure @ Expr {
708- kind : ExprKind :: Closure ( & Closure { fn_decl , .. } ) ,
707+ closure_expr @ Expr {
708+ kind : ExprKind :: Closure ( closure ) ,
709709 ..
710710 } ,
711711 ) = cx. tcx . hir ( ) . get ( owner_id)
712712 {
713- match fn_decl. output {
714- FnRetTy :: Return ( ty) => {
715- if let Some ( sig) = expr_sig ( cx, closure)
716- && let Some ( output) = sig. output ( )
717- {
718- binding_ty_auto_deref_stability ( cx, ty, precedence, output. bound_vars ( ) )
719- } else {
720- Position :: Other ( precedence)
721- }
722- } ,
723- FnRetTy :: DefaultReturn ( _) => Position :: Other ( precedence) ,
724- }
713+ closure_result_position ( cx, closure, cx. typeck_results ( ) . expr_ty ( closure_expr) , precedence)
725714 } else {
726715 let output = cx
727716 . tcx
@@ -730,6 +719,12 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
730719 } ,
731720 )
732721 } ,
722+ ExprKind :: Closure ( closure) => Some ( closure_result_position (
723+ cx,
724+ closure,
725+ cx. typeck_results ( ) . expr_ty ( parent) ,
726+ precedence,
727+ ) ) ,
733728 ExprKind :: Call ( func, _) if func. hir_id == child_id => {
734729 ( child_id == e. hir_id ) . then_some ( Position :: Callee )
735730 } ,
@@ -825,6 +820,26 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
825820 ( position, adjustments)
826821}
827822
823+ fn closure_result_position < ' tcx > (
824+ cx : & LateContext < ' tcx > ,
825+ closure : & ' tcx Closure < ' _ > ,
826+ ty : Ty < ' tcx > ,
827+ precedence : i8 ,
828+ ) -> Position {
829+ match closure. fn_decl . output {
830+ FnRetTy :: Return ( hir_ty) => {
831+ if let Some ( sig) = ty_sig ( cx, ty)
832+ && let Some ( output) = sig. output ( )
833+ {
834+ binding_ty_auto_deref_stability ( cx, hir_ty, precedence, output. bound_vars ( ) )
835+ } else {
836+ Position :: Other ( precedence)
837+ }
838+ } ,
839+ FnRetTy :: DefaultReturn ( _) => Position :: Other ( precedence) ,
840+ }
841+ }
842+
828843// Checks the stability of auto-deref when assigned to a binding with the given explicit type.
829844//
830845// e.g.
0 commit comments