@@ -41,6 +41,7 @@ pub(super) fn check<'tcx>(
4141 if search_snippet. lines ( ) . count ( ) <= 1 {
4242 // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
4343 // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
44+ let mut applicability = Applicability :: MachineApplicable ;
4445 let any_search_snippet = if_chain ! {
4546 if search_method == "find" ;
4647 if let hir:: ExprKind :: Closure ( _, _, body_id, ..) = search_arg. kind;
@@ -52,8 +53,12 @@ pub(super) fn check<'tcx>(
5253 } else if let PatKind :: Binding ( ..) = strip_pat_refs( closure_arg. pat) . kind {
5354 // `find()` provides a reference to the item, but `any` does not,
5455 // 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+ }
5762 } else {
5863 None
5964 }
@@ -73,7 +78,7 @@ pub(super) fn check<'tcx>(
7378 "any({})" ,
7479 any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
7580 ) ,
76- Applicability :: MachineApplicable ,
81+ applicability ,
7782 ) ;
7883 } else {
7984 let iter = snippet ( cx, search_recv. span , ".." ) ;
@@ -88,7 +93,7 @@ pub(super) fn check<'tcx>(
8893 iter,
8994 any_search_snippet. as_ref( ) . map_or( & * search_snippet, String :: as_str)
9095 ) ,
91- Applicability :: MachineApplicable ,
96+ applicability ,
9297 ) ;
9398 }
9499 } else {
@@ -153,14 +158,19 @@ pub(super) fn check<'tcx>(
153158 }
154159}
155160
161+ struct ClosureSugg {
162+ applicability : Applicability ,
163+ suggestion : String ,
164+ }
165+
156166// Build suggestion gradually by handling closure arg specific usages,
157167// such as explicit deref and borrowing cases.
158168// Returns `None` if no such use cases have been triggered in closure body
159169fn get_closure_suggestion < ' tcx > (
160170 cx : & LateContext < ' _ > ,
161171 search_arg : & ' tcx hir:: Expr < ' _ > ,
162172 closure_body : & hir:: Body < ' _ > ,
163- ) -> Option < String > {
173+ ) -> Option < ClosureSugg > {
164174 let mut visitor = DerefDelegate {
165175 cx,
166176 closure_span : search_arg. span ,
@@ -178,7 +188,10 @@ fn get_closure_suggestion<'tcx>(
178188 if visitor. suggestion_start . is_empty ( ) {
179189 None
180190 } else {
181- Some ( visitor. finish ( ) )
191+ Some ( ClosureSugg {
192+ applicability : visitor. applicability ,
193+ suggestion : visitor. finish ( ) ,
194+ } )
182195 }
183196}
184197
0 commit comments