Skip to content

Commit ebc2dfa

Browse files
committed
don't suggest to use cloned for Cow in unnecessary_to_owned
1 parent 0f9cc8d commit ebc2dfa

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ fn check_into_iter_call_arg(
221221
if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) {
222222
return true;
223223
}
224+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(receiver), sym::Cow) {
225+
span_lint_and_sugg(
226+
cx,
227+
UNNECESSARY_TO_OWNED,
228+
parent.span,
229+
format!("unnecessary use of `{method_name}`"),
230+
"use",
231+
format!("{receiver_snippet}.into_owned()"),
232+
Applicability::MaybeIncorrect,
233+
);
234+
return true;
235+
}
236+
224237
let cloned_or_copied = if is_copy(cx, item_ty) && msrv.meets(msrvs::ITERATOR_COPIED) {
225238
"copied"
226239
} else {

tests/ui/unnecessary_to_owned.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,9 @@ fn borrow_checks() {
587587
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
588588
HashSet::<String>::new().get(&1.to_string());
589589
}
590+
591+
fn issue13624() -> impl IntoIterator {
592+
let cow: Cow<'_, Vec<String>> = Cow::Owned(vec![String::from("foo")]);
593+
594+
cow.into_owned().into_iter()
595+
}

tests/ui/unnecessary_to_owned.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,5 +589,11 @@ error: unnecessary use of `to_vec`
589589
LL | s.remove(&(&["b"]).to_vec());
590590
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `(&["b"]).as_slice()`
591591

592-
error: aborting due to 88 previous errors
592+
error: unnecessary use of `into_owned`
593+
--> tests/ui/unnecessary_to_owned.rs:594:5
594+
|
595+
LL | cow.into_owned().into_iter()
596+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cow.into_owned()`
597+
598+
error: aborting due to 89 previous errors
593599

0 commit comments

Comments
 (0)