Skip to content

Commit 3bb6ee5

Browse files
committed
Auto merge of rust-lang#10295 - DesmondWillowbrook:diff-format-suspicious-to-owned, r=Jarcho
fix(suspicious_to_owned): use span_suggestions to suggest both intents fixes rust-lang#10294 changelog: [`suspicious_to_owned`]: suggestions now produce valid Rust code
2 parents 8a98609 + b7c3898 commit 3bb6ee5

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

clippy_lints/src/methods/suspicious_to_owned.rs

+17-5
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,31 @@ 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_suggestion(
33+
expr.span,
34+
"depending on intent, either make the Cow an Owned variant",
35+
format!("{recv_snip}.into_owned()"),
36+
app
37+
);
38+
diag.span_suggestion(
39+
expr.span,
40+
"or clone the Cow itself",
41+
format!("{recv_snip}.clone()"),
42+
app
43+
);
44+
}
3345
);
3446
return true;
3547
}

tests/ui/suspicious_to_owned.stderr

+39-4
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,62 @@ error: this `to_owned` call clones the Cow<'_, str> itself and does not cause th
22
--> $DIR/suspicious_to_owned.rs:16:13
33
|
44
LL | let _ = cow.to_owned();
5-
| ^^^^^^^^^^^^^^ help: consider using, depending on intent: `cow.clone()` or `cow.into_owned()`
5+
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::suspicious-to-owned` implied by `-D warnings`
8+
help: depending on intent, either make the Cow an Owned variant
9+
|
10+
LL | let _ = cow.into_owned();
11+
| ~~~~~~~~~~~~~~~~
12+
help: or clone the Cow itself
13+
|
14+
LL | let _ = cow.clone();
15+
| ~~~~~~~~~~~
816

917
error: this `to_owned` call clones the Cow<'_, [char; 3]> itself and does not cause the Cow<'_, [char; 3]> contents to become owned
1018
--> $DIR/suspicious_to_owned.rs:26:13
1119
|
1220
LL | let _ = cow.to_owned();
13-
| ^^^^^^^^^^^^^^ help: consider using, depending on intent: `cow.clone()` or `cow.into_owned()`
21+
| ^^^^^^^^^^^^^^
22+
|
23+
help: depending on intent, either make the Cow an Owned variant
24+
|
25+
LL | let _ = cow.into_owned();
26+
| ~~~~~~~~~~~~~~~~
27+
help: or clone the Cow itself
28+
|
29+
LL | let _ = cow.clone();
30+
| ~~~~~~~~~~~
1431

1532
error: this `to_owned` call clones the Cow<'_, Vec<char>> itself and does not cause the Cow<'_, Vec<char>> contents to become owned
1633
--> $DIR/suspicious_to_owned.rs:36:13
1734
|
1835
LL | let _ = cow.to_owned();
19-
| ^^^^^^^^^^^^^^ help: consider using, depending on intent: `cow.clone()` or `cow.into_owned()`
36+
| ^^^^^^^^^^^^^^
37+
|
38+
help: depending on intent, either make the Cow an Owned variant
39+
|
40+
LL | let _ = cow.into_owned();
41+
| ~~~~~~~~~~~~~~~~
42+
help: or clone the Cow itself
43+
|
44+
LL | let _ = cow.clone();
45+
| ~~~~~~~~~~~
2046

2147
error: this `to_owned` call clones the Cow<'_, str> itself and does not cause the Cow<'_, str> contents to become owned
2248
--> $DIR/suspicious_to_owned.rs:46:13
2349
|
2450
LL | let _ = cow.to_owned();
25-
| ^^^^^^^^^^^^^^ help: consider using, depending on intent: `cow.clone()` or `cow.into_owned()`
51+
| ^^^^^^^^^^^^^^
52+
|
53+
help: depending on intent, either make the Cow an Owned variant
54+
|
55+
LL | let _ = cow.into_owned();
56+
| ~~~~~~~~~~~~~~~~
57+
help: or clone the Cow itself
58+
|
59+
LL | let _ = cow.clone();
60+
| ~~~~~~~~~~~
2661

2762
error: implicitly cloning a `String` by calling `to_owned` on its dereferenced type
2863
--> $DIR/suspicious_to_owned.rs:60:13

0 commit comments

Comments
 (0)