Fix map_clone with deref and clone

This commit is contained in:
Cameron Steffen 2020-10-30 09:18:16 -05:00
parent 040d0ca4da
commit 769094410a
3 changed files with 23 additions and 5 deletions

View File

@ -8,6 +8,7 @@
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 @@ fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
}
}
},
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

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

View File

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