Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't suggest to use cloned for Cow in unnecessary_to_owned #13853

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clippy_lints/src/methods/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ fn check_into_iter_call_arg(
if unnecessary_iter_cloned::check_for_loop_iter(cx, parent, method_name, receiver, true) {
return true;
}
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(receiver), sym::Cow) {
span_lint_and_sugg(
cx,
UNNECESSARY_TO_OWNED,
parent.span,
format!("unnecessary use of `{method_name}.into_iter()`"),
"use",
format!("{receiver_snippet}.{method_name}()"),
Applicability::MaybeIncorrect,
);
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.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,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()
}
8 changes: 7 additions & 1 deletion tests/ui/unnecessary_to_owned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -589,5 +589,11 @@ error: unnecessary use of `to_vec`
LL | s.remove(&(&["b"]).to_vec());
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `(&["b"]).as_slice()`

error: aborting due to 88 previous errors
error: unnecessary use of `into_owned.into_iter()`
--> tests/ui/unnecessary_to_owned.rs:594:5
|
LL | cow.into_owned().into_iter()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cow.into_owned()`
Comment on lines +595 to +596
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion works in this specific case, but it doesn't seem like it would work in the general case if the iterator is later used as part of another expression, e.g. for cow.into_owned().into_iter().map(...).collect() the .into_iter() is required. Tbh I don't think we can really suggest anything here for Cow::into_owned and we probably should just not lint at all here?


error: aborting due to 89 previous errors

Loading