Skip to content

Commit

Permalink
Only remove one reference in implicit_clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Jan 5, 2022
1 parent 92048f4 commit 0dbe2f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 7 additions & 2 deletions clippy_lints/src/methods/implicit_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty::TyS;
use rustc_middle::ty::{self, TyS};
use rustc_span::{sym, Span};

use super::IMPLICIT_CLONE;
Expand All @@ -14,7 +14,12 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
if is_clone_like(cx, method_name, method_def_id);
let return_type = cx.typeck_results().expr_ty(expr);
let input_type = cx.typeck_results().expr_ty(recv).peel_refs();
let input_type = cx.typeck_results().expr_ty(recv);
// Remove only a single reference. `(&&T)::clone()` returns `&T`
let input_type = match input_type.kind() {
ty::Ref(_, ty, _) => ty,
_ => input_type,
};
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
if TyS::same_type(return_type, input_type);
then {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/implicit_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,10 @@ fn main() {
let os_str = OsStr::new("foo");
let _ = os_str.to_owned();
let _ = os_str.to_os_string();

// issue #8227
let pathbuf_ref = &pathbuf;
let pathbuf_ref = &pathbuf_ref;
let _ = pathbuf_ref.to_owned();
let _ = pathbuf_ref.to_path_buf();
}

0 comments on commit 0dbe2f2

Please sign in to comment.