Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions clippy_lints/src/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::Mutability;
use rustc_middle::ty;
use rustc_middle::ty::adjustment::Adjust;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::Ident;
use rustc_span::{sym, Span};
Expand Down Expand Up @@ -75,11 +76,14 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
}
}
},
hir::ExprKind::MethodCall(ref method, _, ref obj, _) => {
if ident_eq(name, &obj[0]) && method.ident.as_str() == "clone"
&& match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {

let obj_ty = cx.typeck_results().expr_ty(&obj[0]);
hir::ExprKind::MethodCall(ref method, _, [obj], _) => if_chain! {
if ident_eq(name, obj) && method.ident.name == sym::clone;
if match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT);
// no autoderefs
if !cx.typeck_results().expr_adjustments(obj).iter()
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
then {
let obj_ty = cx.typeck_results().expr_ty(obj);
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
if matches!(mutability, Mutability::Not) {
let copy = is_copy(cx, ty);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/map_clone.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ fn main() {
let items = vec![&mut aa, &mut bb];
let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
}

// Issue #6239 deref coercion and clone deref
{
use std::cell::RefCell;

let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
}
}
7 changes: 7 additions & 0 deletions tests/ui/map_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ fn main() {
let items = vec![&mut aa, &mut bb];
let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
}

// Issue #6239 deref coercion and clone deref
{
use std::cell::RefCell;

let _ = Some(RefCell::new(String::new()).borrow()).map(|s| s.clone());
}
}