]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #6301 - alex-700:fix-map-clone, r=matthiaskrgr
authorbors <bors@rust-lang.org>
Sun, 8 Nov 2020 01:28:27 +0000 (01:28 +0000)
committerbors <bors@rust-lang.org>
Sun, 8 Nov 2020 01:28:27 +0000 (01:28 +0000)
do not trigger map_clone in the case of &mut

fixes #6299
changelog: do not trigger map_clone in the case of &mut

clippy_lints/src/map_clone.rs
tests/ui/map_clone.fixed
tests/ui/map_clone.rs

index 034cd99a9be06b072ef7a1e8fdafa8e8a912cf42..9a00608ce3947842bfcf0a1657da3d2b1774c18f 100644 (file)
@@ -80,9 +80,11 @@ fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
                                     && match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {
 
                                     let obj_ty = cx.typeck_results().expr_ty(&obj[0]);
-                                    if let ty::Ref(_, ty, _) = obj_ty.kind() {
-                                        let copy = is_copy(cx, ty);
-                                        lint(cx, e.span, args[0].span, copy);
+                                    if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
+                                        if matches!(mutability, Mutability::Not) {
+                                            let copy = is_copy(cx, ty);
+                                            lint(cx, e.span, args[0].span, copy);
+                                        }
                                     } else {
                                         lint_needless_cloning(cx, e.span, args[0].span);
                                     }
index 81c7f659efb1fed41dbbd9433b9dd5a8c3d79ffe..6e3a8e67e811ebeb5cf99f4add616fcf816dd516 100644 (file)
@@ -44,4 +44,12 @@ fn main() {
         let v = vec![&mut d];
         let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
     }
+
+    // Issue #6299
+    {
+        let mut aa = 5;
+        let mut bb = 3;
+        let items = vec![&mut aa, &mut bb];
+        let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
+    }
 }
index 8ed164f0ed51dabcf0d6e449351e427f6a1576d5..6fd395710d4ba9d91ec44cb8b92666435eac78be 100644 (file)
@@ -44,4 +44,12 @@ fn main() {
         let v = vec![&mut d];
         let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
     }
+
+    // Issue #6299
+    {
+        let mut aa = 5;
+        let mut bb = 3;
+        let items = vec![&mut aa, &mut bb];
+        let _: Vec<_> = items.into_iter().map(|x| x.clone()).collect();
+    }
 }