Skip to content

Commit 8f257c7

Browse files
authored
don't suggest to use cloned for Cow in unnecessary_to_owned (rust-lang#13853)
fix rust-lang#13624 changelog: [`unnecessary_to_owned`]: don't suggest to use `cloned` on `Cow` in `unnecessary_to_owned`
2 parents d648cc9 + 08d8c4a commit 8f257c7

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

clippy_lints/src/methods/unnecessary_to_owned.rs

+3
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,13 @@ fn check_into_iter_call_arg(
217217
&& implements_trait(cx, parent_ty, iterator_trait_id, &[])
218218
&& let Some(item_ty) = get_iterator_item_ty(cx, parent_ty)
219219
&& let Some(receiver_snippet) = receiver.span.get_source_text(cx)
220+
// If the receiver is a `Cow`, we can't remove the `into_owned` generally, see https://github.com/rust-lang/rust-clippy/issues/13624.
221+
&& !is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(receiver), sym::Cow)
220222
{
221223
if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) {
222224
return true;
223225
}
226+
224227
let cloned_or_copied = if is_copy(cx, item_ty) && msrv.meets(msrvs::ITERATOR_COPIED) {
225228
"copied"
226229
} else {

tests/ui/unnecessary_to_owned.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,9 @@ fn borrow_checks() {
585585
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
586586
HashSet::<String>::new().get(&1.to_string());
587587
}
588+
589+
fn issue13624() -> impl IntoIterator {
590+
let cow: Cow<'_, Vec<String>> = Cow::Owned(vec![String::from("foo")]);
591+
592+
cow.into_owned().into_iter()
593+
}

tests/ui/unnecessary_to_owned.rs

+6
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,9 @@ fn borrow_checks() {
585585
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
586586
HashSet::<String>::new().get(&1.to_string());
587587
}
588+
589+
fn issue13624() -> impl IntoIterator {
590+
let cow: Cow<'_, Vec<String>> = Cow::Owned(vec![String::from("foo")]);
591+
592+
cow.into_owned().into_iter()
593+
}

0 commit comments

Comments
 (0)