Don't lint implicit_clone when the type doesn't implement clone

This commit is contained in:
Jason Newcomb 2022-12-01 23:05:53 -05:00
parent d822110d3b
commit ab8c6beb85
3 changed files with 23 additions and 1 deletions

View File

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_context;
use clippy_utils::ty::peel_mid_ty_refs;
use clippy_utils::ty::{implements_trait, peel_mid_ty_refs};
use clippy_utils::{is_diag_item_method, is_diag_trait_item};
use if_chain::if_chain;
use rustc_errors::Applicability;
@ -19,6 +19,8 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
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();
if implements_trait(cx, return_type, clone_trait, &[]);
then {
let mut app = Applicability::MachineApplicable;
let recv_snip = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut app).0;

View File

@ -115,4 +115,14 @@ fn main() {
let pathbuf_ref = &pathbuf_ref;
let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf`
let _ = (**pathbuf_ref).clone();
struct NoClone;
impl ToOwned for NoClone {
type Owned = Self;
fn to_owned(&self) -> Self {
NoClone
}
}
let no_clone = &NoClone;
let _ = no_clone.to_owned();
}

View File

@ -115,4 +115,14 @@ fn main() {
let pathbuf_ref = &pathbuf_ref;
let _ = pathbuf_ref.to_owned(); // Don't lint. Returns `&&PathBuf`
let _ = pathbuf_ref.to_path_buf();
struct NoClone;
impl ToOwned for NoClone {
type Owned = Self;
fn to_owned(&self) -> Self {
NoClone
}
}
let no_clone = &NoClone;
let _ = no_clone.to_owned();
}