1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: source:: snippet_with_applicability;
3
3
use 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
+ } ;
5
7
use rustc_errors:: Applicability ;
6
8
use rustc_hir as hir;
7
9
use rustc_lint:: LateContext ;
@@ -108,26 +110,12 @@ fn check_qpath(cx: &LateContext<'_>, qpath: hir::QPath<'_>, hir_id: hir::HirId)
108
110
109
111
fn is_calling_clone ( cx : & LateContext < ' _ > , arg : & hir:: Expr < ' _ > ) -> bool {
110
112
match arg. kind {
111
- hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } ) => {
113
+ hir:: ExprKind :: Closure ( & hir:: Closure { body, .. } )
112
114
// 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
+ {
131
119
let closure_expr = peel_blocks ( closure_body. value ) ;
132
120
match closure_expr. kind {
133
121
hir:: ExprKind :: MethodCall ( method, obj, [ ] , _) => {
@@ -139,7 +127,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
139
127
// no autoderefs
140
128
&& !cx. typeck_results ( ) . expr_adjustments ( obj) . iter ( )
141
129
. any ( |a| matches ! ( a. kind, Adjust :: Deref ( Some ( ..) ) ) )
142
- && path_to_local_id ( obj, * local_id)
130
+ && path_to_local_id ( obj, local_id)
143
131
{
144
132
true
145
133
} else {
@@ -148,7 +136,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
148
136
} ,
149
137
hir:: ExprKind :: Call ( call, [ recv] ) => {
150
138
if let hir:: ExprKind :: Path ( qpath) = call. kind
151
- && path_to_local_id ( recv, * local_id)
139
+ && path_to_local_id ( recv, local_id)
152
140
{
153
141
check_qpath ( cx, qpath, call. hir_id )
154
142
} else {
0 commit comments