about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibsG <thibsg@pm.me>2021-09-07 12:31:14 +0200
committerThibsG <thibsg@pm.me>2021-11-20 09:40:11 +0100
commit97783a8cb9b154b17abbf0084fe8a16d490cf801 (patch)
tree9641fd5c15272f9c40b76df0ce0a1f4d18e44cfb
parentd0dd797709540f460ee6c9ac26f1231335781b40 (diff)
downloadrust-97783a8cb9b154b17abbf0084fe8a16d490cf801.tar.gz
rust-97783a8cb9b154b17abbf0084fe8a16d490cf801.zip
Return a struct and add applicability
-rw-r--r--clippy_lints/src/methods/search_is_some.rs25
1 files changed, 19 insertions, 6 deletions
diff --git a/clippy_lints/src/methods/search_is_some.rs b/clippy_lints/src/methods/search_is_some.rs
index e793c50ac9d..a1206199460 100644
--- a/clippy_lints/src/methods/search_is_some.rs
+++ b/clippy_lints/src/methods/search_is_some.rs
@@ -41,6 +41,7 @@ pub(super) fn check<'tcx>(
         if search_snippet.lines().count() <= 1 {
             // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
             // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
+            let mut applicability = Applicability::MachineApplicable;
             let any_search_snippet = if_chain! {
                 if search_method == "find";
                 if let hir::ExprKind::Closure(_, _, body_id, ..) = search_arg.kind;
@@ -52,8 +53,12 @@ pub(super) fn check<'tcx>(
                     } else if let PatKind::Binding(..) = strip_pat_refs(closure_arg.pat).kind {
                         // `find()` provides a reference to the item, but `any` does not,
                         // so we should fix item usages for suggestion
-                        get_closure_suggestion(cx, search_arg, closure_body)
-                            .or_else(|| Some(search_snippet.to_string()))
+                        if let Some(closure_sugg) = get_closure_suggestion(cx, search_arg, closure_body) {
+                            applicability = closure_sugg.applicability;
+                            Some(closure_sugg.suggestion)
+                        } else {
+                            Some(search_snippet.to_string())
+                        }
                     } else {
                         None
                     }
@@ -73,7 +78,7 @@ pub(super) fn check<'tcx>(
                         "any({})",
                         any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
                     ),
-                    Applicability::MachineApplicable,
+                    applicability,
                 );
             } else {
                 let iter = snippet(cx, search_recv.span, "..");
@@ -88,7 +93,7 @@ pub(super) fn check<'tcx>(
                         iter,
                         any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
                     ),
-                    Applicability::MachineApplicable,
+                    applicability,
                 );
             }
         } else {
@@ -153,6 +158,11 @@ pub(super) fn check<'tcx>(
     }
 }
 
+struct ClosureSugg {
+    applicability: Applicability,
+    suggestion: String,
+}
+
 // Build suggestion gradually by handling closure arg specific usages,
 // such as explicit deref and borrowing cases.
 // Returns `None` if no such use cases have been triggered in closure body
@@ -160,7 +170,7 @@ fn get_closure_suggestion<'tcx>(
     cx: &LateContext<'_>,
     search_arg: &'tcx hir::Expr<'_>,
     closure_body: &hir::Body<'_>,
-) -> Option<String> {
+) -> Option<ClosureSugg> {
     let mut visitor = DerefDelegate {
         cx,
         closure_span: search_arg.span,
@@ -178,7 +188,10 @@ fn get_closure_suggestion<'tcx>(
     if visitor.suggestion_start.is_empty() {
         None
     } else {
-        Some(visitor.finish())
+        Some(ClosureSugg {
+            applicability: visitor.applicability,
+            suggestion: visitor.finish(),
+        })
     }
 }