Skip to content

Commit

Permalink
don't call iter() on a temporary object in unnecessary_to_owned
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Feb 17, 2025
1 parent 8cef0b6 commit d8286f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clippy_lints/src/methods/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ fn check_into_iter_call_arg(
&& 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)
// Calling `iter()` on a temporary object can lead to false positives. #14242
&& !matches!(receiver.kind, ExprKind::Call(..) | ExprKind::Struct(..) | ExprKind::Tup(..))
{
if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) {
return true;
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/unnecessary_to_owned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,18 @@ fn issue13624() -> impl IntoIterator {

cow.into_owned().into_iter()
}

mod issue_14242 {
use std::rc::Rc;

#[derive(Copy, Clone)]
struct Foo;

fn rc_slice_provider() -> Rc<[Foo]> {
Rc::from([Foo])
}

fn iterator_provider() -> impl Iterator<Item = Foo> {
rc_slice_provider().to_vec().into_iter()
}
}
15 changes: 15 additions & 0 deletions tests/ui/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,18 @@ fn issue13624() -> impl IntoIterator {

cow.into_owned().into_iter()
}

mod issue_14242 {
use std::rc::Rc;

#[derive(Copy, Clone)]
struct Foo;

fn rc_slice_provider() -> Rc<[Foo]> {
Rc::from([Foo])
}

fn iterator_provider() -> impl Iterator<Item = Foo> {
rc_slice_provider().to_vec().into_iter()
}
}

0 comments on commit d8286f5

Please sign in to comment.