11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use clippy_utils:: source:: snippet_with_applicability;
33use clippy_utils:: ty:: walk_ptrs_ty_depth;
4- use clippy_utils:: { get_parent_expr, is_diag_trait_item, match_def_path, path_to_local_id, paths, peel_blocks} ;
4+ use clippy_utils:: {
5+ get_parent_expr, is_diag_trait_item, match_def_path, path_to_local_id, paths, peel_blocks, strip_pat_refs,
6+ } ;
57use rustc_errors:: Applicability ;
68use rustc_hir as hir;
79use rustc_lint:: LateContext ;
@@ -108,26 +110,12 @@ fn check_qpath(cx: &LateContext<'_>, qpath: hir::QPath<'_>, hir_id: hir::HirId)
108110
109111fn is_calling_clone ( cx : & LateContext < ' _ > , arg : & hir:: Expr < ' _ > ) -> bool {
110112 match arg. kind {
111- hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } ) => {
113+ hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } )
112114 // If it's a closure, we need to check what is called.
113- let closure_body = cx. tcx . hir ( ) . body ( body) ;
114-
115- // |x| ...
116- // ^
117- let [
118- hir:: Param {
119- pat :
120- hir:: Pat {
121- kind : hir:: PatKind :: Binding ( _, local_id, ..) ,
122- ..
123- } ,
124- ..
125- } ,
126- ] = closure_body. params
127- else {
128- return false ;
129- } ;
130-
115+ if let closure_body = cx. tcx . hir ( ) . body ( body)
116+ && let [ param] = closure_body. params
117+ && let hir:: PatKind :: Binding ( _, local_id, ..) = strip_pat_refs ( param. pat ) . kind =>
118+ {
131119 let closure_expr = peel_blocks ( closure_body. value ) ;
132120 match closure_expr. kind {
133121 hir:: ExprKind :: MethodCall ( method, obj, [ ] , _) => {
@@ -139,7 +127,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
139127 // no autoderefs
140128 && !cx. typeck_results ( ) . expr_adjustments ( obj) . iter ( )
141129 . any ( |a| matches ! ( a. kind, Adjust :: Deref ( Some ( ..) ) ) )
142- && path_to_local_id ( obj, * local_id)
130+ && path_to_local_id ( obj, local_id)
143131 {
144132 true
145133 } else {
@@ -148,7 +136,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
148136 } ,
149137 hir:: ExprKind :: Call ( call, [ recv] ) => {
150138 if let hir:: ExprKind :: Path ( qpath) = call. kind
151- && path_to_local_id ( recv, * local_id)
139+ && path_to_local_id ( recv, local_id)
152140 {
153141 check_qpath ( cx, qpath, call. hir_id )
154142 } else {
0 commit comments