Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1fb42da

Browse files
committed
use span_suggestions to suggest both intents
1 parent 8a98609 commit 1fb42da

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

clippy_lints/src/methods/suspicious_to_owned.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_diag_trait_item;
33
use clippy_utils::source::snippet_with_context;
44
use if_chain::if_chain;
@@ -17,19 +17,25 @@ pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) -
1717
let input_type = cx.typeck_results().expr_ty(expr);
1818
if let ty::Adt(adt, _) = cx.typeck_results().expr_ty(expr).kind();
1919
if cx.tcx.is_diagnostic_item(sym::Cow, adt.did());
20+
2021
then {
2122
let mut app = Applicability::MaybeIncorrect;
2223
let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;
23-
span_lint_and_sugg(
24+
span_lint_and_then(
2425
cx,
2526
SUSPICIOUS_TO_OWNED,
2627
expr.span,
2728
&with_forced_trimmed_paths!(format!(
2829
"this `to_owned` call clones the {input_type} itself and does not cause the {input_type} contents to become owned"
2930
)),
30-
"consider using, depending on intent",
31-
format!("{recv_snip}.clone()` or `{recv_snip}.into_owned()"),
32-
app,
31+
|diag| {
32+
diag.span_suggestions(
33+
expr.span,
34+
"depending on intent, either make the Cow an Owned variant or clone the Cow itself",
35+
[format!("{recv_snip}.into_owned()"), format!("{recv_snip}.clone()")],
36+
Applicability::Unspecified
37+
);
38+
}
3339
);
3440
return true;
3541
}

0 commit comments

Comments
 (0)