about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKartavya Vashishtha <sendtokartavya@gmail.com>2023-02-05 01:05:31 +0530
committerKartavya Vashishtha <sendtokartavya@gmail.com>2023-02-05 01:05:31 +0530
commit1fb42daf11e68393f2cf9eb8e67e4b2779a4bf5e (patch)
tree70af7339d036d0a0bfeb1c620226237f0f51b33e
parent8a9860901f0ae9782ff23fb793838a16f733a60b (diff)
downloadrust-1fb42daf11e68393f2cf9eb8e67e4b2779a4bf5e.tar.gz
rust-1fb42daf11e68393f2cf9eb8e67e4b2779a4bf5e.zip
use span_suggestions to suggest both intents
-rw-r--r--clippy_lints/src/methods/suspicious_to_owned.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/clippy_lints/src/methods/suspicious_to_owned.rs b/clippy_lints/src/methods/suspicious_to_owned.rs
index fe88fa41fd9..1adc7b7453e 100644
--- a/clippy_lints/src/methods/suspicious_to_owned.rs
+++ b/clippy_lints/src/methods/suspicious_to_owned.rs
@@ -1,4 +1,4 @@
-use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::is_diag_trait_item;
 use clippy_utils::source::snippet_with_context;
 use if_chain::if_chain;
@@ -17,19 +17,25 @@ pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) -
         let input_type = cx.typeck_results().expr_ty(expr);
         if let ty::Adt(adt, _) = cx.typeck_results().expr_ty(expr).kind();
         if cx.tcx.is_diagnostic_item(sym::Cow, adt.did());
+
         then {
             let mut app = Applicability::MaybeIncorrect;
             let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;
-            span_lint_and_sugg(
+            span_lint_and_then(
                 cx,
                 SUSPICIOUS_TO_OWNED,
                 expr.span,
                 &with_forced_trimmed_paths!(format!(
                     "this `to_owned` call clones the {input_type} itself and does not cause the {input_type} contents to become owned"
                 )),
-                "consider using, depending on intent",
-                format!("{recv_snip}.clone()` or `{recv_snip}.into_owned()"),
-                app,
+                |diag| {
+                    diag.span_suggestions(
+                        expr.span,
+                        "depending on intent, either make the Cow an Owned variant or clone the Cow itself",
+                        [format!("{recv_snip}.into_owned()"), format!("{recv_snip}.clone()")],
+                        Applicability::Unspecified
+                    );
+                }
             );
             return true;
         }