Skip to content

Commit 11efa01

Browse files
committed
Auto merge of #11321 - J-ZhengLi:issue11281, r=giraffate
allow calling `to_owned` on borrowed value for [`implicit_clone`] fixes: #11281 a small and simple fix that give up checking for `referenced_value.to_owned()` usage. changelog: allow calling `to_owned` with borrowed value for [`implicit_clone`]
2 parents b5bfd11 + aa8995e commit 11efa01

File tree

3 files changed

+3
-8
lines changed

3 files changed

+3
-8
lines changed

clippy_lints/src/methods/implicit_clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
1717
let return_type = cx.typeck_results().expr_ty(expr);
1818
let input_type = cx.typeck_results().expr_ty(recv);
1919
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
20+
if !(ref_count > 0 && is_diag_trait_item(cx, method_def_id, sym::ToOwned));
2021
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()));
2122
if return_type == input_type;
2223
if let Some(clone_trait) = cx.tcx.lang_items().clone_trait();

tests/ui/implicit_clone.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn main() {
6767

6868
let vec_ref = &vec;
6969
let _ = return_owned_from_slice(vec_ref);
70-
let _ = vec_ref.clone();
70+
let _ = vec_ref.to_owned();
7171
let _ = vec_ref.clone();
7272

7373
// we expect no lint for this

tests/ui/implicit_clone.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
1212
LL | let _ = vec.to_vec();
1313
| ^^^^^^^^^^^^ help: consider using: `vec.clone()`
1414

15-
error: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type
16-
--> $DIR/implicit_clone.rs:70:13
17-
|
18-
LL | let _ = vec_ref.to_owned();
19-
| ^^^^^^^^^^^^^^^^^^ help: consider using: `vec_ref.clone()`
20-
2115
error: implicitly cloning a `Vec` by calling `to_vec` on its dereferenced type
2216
--> $DIR/implicit_clone.rs:71:13
2317
|
@@ -72,5 +66,5 @@ error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenc
7266
LL | let _ = pathbuf_ref.to_path_buf();
7367
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(**pathbuf_ref).clone()`
7468

75-
error: aborting due to 12 previous errors
69+
error: aborting due to 11 previous errors
7670

0 commit comments

Comments
 (0)