@@ -50,6 +50,8 @@ pub(super) fn check<'tcx>(
50
50
if let hir:: PatKind :: Ref ( ..) = closure_arg. pat. kind {
51
51
Some ( search_snippet. replacen( '&' , "" , 1 ) )
52
52
} else if let PatKind :: Binding ( ..) = strip_pat_refs( closure_arg. pat) . kind {
53
+ // `find()` provides a reference to the item, but `any` does not,
54
+ // so we should fix item usages for suggestion
53
55
get_closure_suggestion( cx, search_arg, closure_body)
54
56
. or_else( || Some ( search_snippet. to_string( ) ) )
55
57
} else {
@@ -151,6 +153,9 @@ pub(super) fn check<'tcx>(
151
153
}
152
154
}
153
155
156
+ // Build suggestion gradually by handling closure arg specific usages,
157
+ // such as explicit deref and borrowing cases.
158
+ // Returns `None` if no such use cases have been triggered in closure body
154
159
fn get_closure_suggestion < ' tcx > (
155
160
cx : & LateContext < ' _ > ,
156
161
search_arg : & ' tcx hir:: Expr < ' _ > ,
@@ -203,8 +208,12 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
203
208
let end_snip = snippet ( self . cx , end_span, ".." ) ;
204
209
205
210
if cmt. place . projections . is_empty ( ) {
211
+ // handle item without any projection, that needs an explicit borrowing
212
+ // i.e.: suggest `&x` instead of `x`
206
213
self . suggestion_start . push_str ( & format ! ( "{}&{}" , start_snip, ident_str) ) ;
207
214
} else {
215
+ // cases where a parent call is using the item
216
+ // i.e.: suggest `.contains(&x)` for `.find(|x| [1, 2, 3].contains(x)).is_none()`
208
217
let parent_expr = get_parent_expr_for_hir ( self . cx , cmt. hir_id ) ;
209
218
if let Some ( Expr { hir_id : _, kind, .. } ) = parent_expr {
210
219
if let ExprKind :: Call ( _, args) | ExprKind :: MethodCall ( _, _, args, _) = kind {
@@ -230,6 +239,8 @@ impl<'tcx> Delegate<'tcx> for DerefDelegate<'_, 'tcx> {
230
239
}
231
240
}
232
241
242
+ // handle item projections by removing one explicit deref
243
+ // i.e.: suggest `*x` instead of `**x`
233
244
let mut replacement_str = ident_str;
234
245
let last_deref = cmt
235
246
. place
0 commit comments