1
1
use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_hir_and_then} ;
2
2
use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
3
3
use 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} ;
5
5
use clippy_utils:: { get_parent_expr, is_lint_allowed, path_to_local, walk_to_expr_usage} ;
6
6
use rustc_ast:: util:: parser:: { PREC_POSTFIX , PREC_PREFIX } ;
7
7
use rustc_data_structures:: fx:: FxIndexMap ;
@@ -704,24 +704,13 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
704
704
let owner_id = cx. tcx . hir ( ) . body_owner ( cx. enclosing_body . unwrap ( ) ) ;
705
705
Some (
706
706
if let Node :: Expr (
707
- closure @ Expr {
708
- kind : ExprKind :: Closure ( & Closure { fn_decl , .. } ) ,
707
+ closure_expr @ Expr {
708
+ kind : ExprKind :: Closure ( closure ) ,
709
709
..
710
710
} ,
711
711
) = cx. tcx . hir ( ) . get ( owner_id)
712
712
{
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)
725
714
} else {
726
715
let output = cx
727
716
. tcx
@@ -730,6 +719,12 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
730
719
} ,
731
720
)
732
721
} ,
722
+ ExprKind :: Closure ( closure) => Some ( closure_result_position (
723
+ cx,
724
+ closure,
725
+ cx. typeck_results ( ) . expr_ty ( parent) ,
726
+ precedence,
727
+ ) ) ,
733
728
ExprKind :: Call ( func, _) if func. hir_id == child_id => {
734
729
( child_id == e. hir_id ) . then_some ( Position :: Callee )
735
730
} ,
@@ -825,6 +820,26 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
825
820
( position, adjustments)
826
821
}
827
822
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
+
828
843
// Checks the stability of auto-deref when assigned to a binding with the given explicit type.
829
844
//
830
845
// e.g.
0 commit comments