Skip to content

allow calling to_owned on borrowed value for [implicit_clone] #11321

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

Merged
merged 1 commit into from
Aug 15, 2023
Merged
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
1 change: 1 addition & 0 deletions clippy_lints/src/methods/implicit_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
let return_type = cx.typeck_results().expr_ty(expr);
let input_type = cx.typeck_results().expr_ty(recv);
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
if !(ref_count > 0 && is_diag_trait_item(cx, method_def_id, sym::ToOwned));
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()));
if return_type == input_type;
if let Some(clone_trait) = cx.tcx.lang_items().clone_trait();
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/implicit_clone.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn main() {

let vec_ref = &vec;
let _ = return_owned_from_slice(vec_ref);
let _ = vec_ref.clone();
let _ = vec_ref.to_owned();
let _ = vec_ref.clone();

// we expect no lint for this
Expand Down
8 changes: 1 addition & 7 deletions tests/ui/implicit_clone.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
LL | let _ = vec.to_vec();
| ^^^^^^^^^^^^ help: consider using: `vec.clone()`

error: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type
--> $DIR/implicit_clone.rs:70:13
|
LL | let _ = vec_ref.to_owned();
| ^^^^^^^^^^^^^^^^^^ help: consider using: `vec_ref.clone()`

error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
--> $DIR/implicit_clone.rs:71:13
|
Expand Down Expand Up @@ -72,5 +66,5 @@ error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenc
LL | let _ = pathbuf_ref.to_path_buf();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(**pathbuf_ref).clone()`

error: aborting due to 12 previous errors
error: aborting due to 11 previous errors