Auto merge of #6269 - camsteffen:map-clone-deref, r=ebroto

Fix map_clone with deref and clone

changelog: Fix map_clone false positive with deref coercion

Fixes #6239
This commit is contained in:
bors 2020-11-11 00:48:33 +00:00
commit c4fc076e11
3 changed files with 23 additions and 5 deletions

View file

@ -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};
@ -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);

View file

@ -53,4 +53,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());
}
}

View file

@ -53,4 +53,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());
}
}