@@ -41,6 +41,7 @@ pub(super) fn check<'tcx>(
41
41
if search_snippet. lines ( ) . count ( ) <= 1 {
42
42
// suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
43
43
// suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
44
+ let mut applicability = Applicability :: MachineApplicable ;
44
45
let any_search_snippet = if_chain ! {
45
46
if search_method == "find" ;
46
47
if let hir:: ExprKind :: Closure ( _, _, body_id, ..) = search_arg. kind;
@@ -52,8 +53,12 @@ pub(super) fn check<'tcx>(
52
53
} else if let PatKind :: Binding ( ..) = strip_pat_refs( closure_arg. pat) . kind {
53
54
// `find()` provides a reference to the item, but `any` does not,
54
55
// so we should fix item usages for suggestion
55
- get_closure_suggestion( cx, search_arg, closure_body)
56
- . or_else( || Some ( search_snippet. to_string( ) ) )
56
+ if let Some ( closure_sugg) = get_closure_suggestion( cx, search_arg, closure_body) {
57
+ applicability = closure_sugg. applicability;
58
+ Some ( closure_sugg. suggestion)
59
+ } else {
60
+ Some ( search_snippet. to_string( ) )
61
+ }
57
62
} else {
58
63
None
59
64
}
@@ -73,7 +78,7 @@ pub(super) fn check<'tcx>(
73
78
"any({})" ,
74
79
any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
75
80
) ,
76
- Applicability :: MachineApplicable ,
81
+ applicability ,
77
82
) ;
78
83
} else {
79
84
let iter = snippet ( cx, search_recv. span , ".." ) ;
@@ -88,7 +93,7 @@ pub(super) fn check<'tcx>(
88
93
iter,
89
94
any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
90
95
) ,
91
- Applicability :: MachineApplicable ,
96
+ applicability ,
92
97
) ;
93
98
}
94
99
} else {
@@ -153,14 +158,19 @@ pub(super) fn check<'tcx>(
153
158
}
154
159
}
155
160
161
+ struct ClosureSugg {
162
+ applicability : Applicability ,
163
+ suggestion : String ,
164
+ }
165
+
156
166
// Build suggestion gradually by handling closure arg specific usages,
157
167
// such as explicit deref and borrowing cases.
158
168
// Returns `None` if no such use cases have been triggered in closure body
159
169
fn get_closure_suggestion < ' tcx > (
160
170
cx : & LateContext < ' _ > ,
161
171
search_arg : & ' tcx hir:: Expr < ' _ > ,
162
172
closure_body : & hir:: Body < ' _ > ,
163
- ) -> Option < String > {
173
+ ) -> Option < ClosureSugg > {
164
174
let mut visitor = DerefDelegate {
165
175
cx,
166
176
closure_span : search_arg. span ,
@@ -178,7 +188,10 @@ fn get_closure_suggestion<'tcx>(
178
188
if visitor. suggestion_start . is_empty ( ) {
179
189
None
180
190
} else {
181
- Some ( visitor. finish ( ) )
191
+ Some ( ClosureSugg {
192
+ applicability : visitor. applicability ,
193
+ suggestion : visitor. finish ( ) ,
194
+ } )
182
195
}
183
196
}
184
197
0 commit comments