Skip to content

Commit ab8c6be

Browse files
committed
Don't lint implicit_clone when the type doesn't implement clone
1 parent d822110 commit ab8c6be

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

clippy_lints/src/methods/implicit_clone.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
3-
use clippy_utils::ty::peel_mid_ty_refs;
3+
use clippy_utils::ty::{implements_trait, peel_mid_ty_refs};
44
use clippy_utils::{is_diag_item_method, is_diag_trait_item};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
@@ -19,6 +19,8 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
1919
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
2020
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()));
2121
if return_type == input_type;
22+
if let Some(clone_trait) = cx.tcx.lang_items().clone_trait();
23+
if implements_trait(cx, return_type, clone_trait, &[]);
2224
then {
2325
let mut app = Applicability::MachineApplicable;
2426
let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;

tests/ui/implicit_clone.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,14 @@ fn main() {
115115
let pathbuf_ref = &pathbuf_ref;
116116
let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf`
117117
let _ = (**pathbuf_ref).clone();
118+
119+
struct NoClone;
120+
impl ToOwned for NoClone {
121+
type Owned = Self;
122+
fn to_owned(&self) -> Self {
123+
NoClone
124+
}
125+
}
126+
let no_clone = &NoClone;
127+
let _ = no_clone.to_owned();
118128
}

tests/ui/implicit_clone.rs

+10
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,14 @@ fn main() {
115115
let pathbuf_ref = &pathbuf_ref;
116116
let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf`
117117
let _ = pathbuf_ref.to_path_buf();
118+
119+
struct NoClone;
120+
impl ToOwned for NoClone {
121+
type Owned = Self;
122+
fn to_owned(&self) -> Self {
123+
NoClone
124+
}
125+
}
126+
let no_clone = &NoClone;
127+
let _ = no_clone.to_owned();
118128
}

0 commit comments

Comments
 (0)