Skip to content

Commit

Permalink
don't suggest to use cloned for Cow in unnecessary_to_owned (#1…
Browse files Browse the repository at this point in the history
…3853)

fix #13624

changelog: [`unnecessary_to_owned`]: don't suggest to use `cloned` on
`Cow` in `unnecessary_to_owned`
  • Loading branch information
y21 authored Jan 12, 2025
2 parents d648cc9 + 08d8c4a commit 8f257c7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clippy_lints/src/methods/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,13 @@ fn check_into_iter_call_arg(
&& implements_trait(cx, parent_ty, iterator_trait_id, &[])
&& let Some(item_ty) = get_iterator_item_ty(cx, parent_ty)
&& let Some(receiver_snippet) = receiver.span.get_source_text(cx)
// If the receiver is a `Cow`, we can't remove the `into_owned` generally, see https://github.com/rust-lang/rust-clippy/issues/13624.
&& !is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(receiver), sym::Cow)
{
if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) {
return true;
}

let cloned_or_copied = if is_copy(cx, item_ty) && msrv.meets(msrvs::ITERATOR_COPIED) {
"copied"
} else {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/unnecessary_to_owned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,9 @@ fn borrow_checks() {
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
HashSet::<String>::new().get(&1.to_string());
}

fn issue13624() -> impl IntoIterator {
let cow: Cow<'_, Vec<String>> = Cow::Owned(vec![String::from("foo")]);

cow.into_owned().into_iter()
}
6 changes: 6 additions & 0 deletions tests/ui/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,9 @@ fn borrow_checks() {
HashSet::<i32>::new().foo::<&str>(&"".to_owned());
HashSet::<String>::new().get(&1.to_string());
}

fn issue13624() -> impl IntoIterator {
let cow: Cow<'_, Vec<String>> = Cow::Owned(vec![String::from("foo")]);

cow.into_owned().into_iter()
}

0 comments on commit 8f257c7

Please sign in to comment.