Skip to content

Commit 0dbe2f2

Browse files
committed
Only remove one reference in implicit_clone
1 parent 92048f4 commit 0dbe2f2

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

clippy_lints/src/methods/implicit_clone.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use if_chain::if_chain;
44
use rustc_errors::Applicability;
55
use rustc_hir as hir;
66
use rustc_lint::LateContext;
7-
use rustc_middle::ty::TyS;
7+
use rustc_middle::ty::{self, TyS};
88
use rustc_span::{sym, Span};
99

1010
use super::IMPLICIT_CLONE;
@@ -14,7 +14,12 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
1414
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
1515
if is_clone_like(cx, method_name, method_def_id);
1616
let return_type = cx.typeck_results().expr_ty(expr);
17-
let input_type = cx.typeck_results().expr_ty(recv).peel_refs();
17+
let input_type = cx.typeck_results().expr_ty(recv);
18+
// Remove only a single reference. `(&&T)::clone()` returns `&T`
19+
let input_type = match input_type.kind() {
20+
ty::Ref(_, ty, _) => ty,
21+
_ => input_type,
22+
};
1823
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
1924
if TyS::same_type(return_type, input_type);
2025
then {

tests/ui/implicit_clone.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ fn main() {
105105
let os_str = OsStr::new("foo");
106106
let _ = os_str.to_owned();
107107
let _ = os_str.to_os_string();
108+
109+
// issue #8227
110+
let pathbuf_ref = &pathbuf;
111+
let pathbuf_ref = &pathbuf_ref;
112+
let _ = pathbuf_ref.to_owned();
113+
let _ = pathbuf_ref.to_path_buf();
108114
}

0 commit comments

Comments
 (0)