Skip to content

Commit

Permalink
don't suggest to use cloned for Cow in unnecessary_to_owned
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Dec 19, 2024
1 parent 0f9cc8d commit ebc2dfa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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}`"),
"use",
format!("{receiver_snippet}.into_owned()"),
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`
--> tests/ui/unnecessary_to_owned.rs:594:5
|
LL | cow.into_owned().into_iter()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `cow.into_owned()`

error: aborting due to 89 previous errors

0 comments on commit ebc2dfa

Please sign in to comment.