about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-04-25 21:16:06 +0000
committerbors <bors@rust-lang.org>2020-04-25 21:16:06 +0000
commita76bfd46c5596cb90465c3accad09250e8204dad (patch)
treec2565dcb52ca538fb6a1a4b6ef6a00448a7de637
parent77c23b70897a097cadd5fdd96644384313243f14 (diff)
parent806d973adcc1459df50dadd088928503f44be24f (diff)
downloadrust-a76bfd46c5596cb90465c3accad09250e8204dad.tar.gz
rust-a76bfd46c5596cb90465c3accad09250e8204dad.zip
Auto merge of #5530 - ebroto:issue_5524, r=flip1995
map_clone: avoid suggesting `copied()` for &mut

changelog: map_clone: avoid suggesting `copied()` for &mut

Fixes #5524
-rw-r--r--clippy_lints/src/map_clone.rs5
-rw-r--r--tests/ui/map_clone.fixed11
-rw-r--r--tests/ui/map_clone.rs11
-rw-r--r--tests/ui/map_clone.stderr12
4 files changed, 31 insertions, 8 deletions
diff --git a/clippy_lints/src/map_clone.rs b/clippy_lints/src/map_clone.rs
index 5c5cf8015f4..0b346393ac3 100644
--- a/clippy_lints/src/map_clone.rs
+++ b/clippy_lints/src/map_clone.rs
@@ -7,6 +7,7 @@ use rustc_ast::ast::Ident;
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::mir::Mutability;
 use rustc_middle::ty;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::source_map::Span;
@@ -58,7 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
             let closure_expr = remove_blocks(&closure_body.value);
             then {
                 match closure_body.params[0].pat.kind {
-                    hir::PatKind::Ref(ref inner, _) => if let hir::PatKind::Binding(
+                    hir::PatKind::Ref(ref inner, hir::Mutability::Not) => if let hir::PatKind::Binding(
                         hir::BindingAnnotation::Unannotated, .., name, None
                     ) = inner.kind {
                         if ident_eq(name, closure_expr) {
@@ -69,7 +70,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
                         match closure_expr.kind {
                             hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
                                 if ident_eq(name, inner) {
-                                    if let ty::Ref(..) = cx.tables.expr_ty(inner).kind {
+                                    if let ty::Ref(.., Mutability::Not) = cx.tables.expr_ty(inner).kind {
                                         lint(cx, e.span, args[0].span, true);
                                     }
                                 }
diff --git a/tests/ui/map_clone.fixed b/tests/ui/map_clone.fixed
index 5e6b3fad41c..81c7f659efb 100644
--- a/tests/ui/map_clone.fixed
+++ b/tests/ui/map_clone.fixed
@@ -4,6 +4,7 @@
 #![allow(clippy::clone_on_copy, clippy::redundant_clone)]
 #![allow(clippy::missing_docs_in_private_items)]
 #![allow(clippy::redundant_closure_for_method_calls)]
+#![allow(clippy::many_single_char_names)]
 
 fn main() {
     let _: Vec<i8> = vec![5_i8; 6].iter().copied().collect();
@@ -33,4 +34,14 @@ fn main() {
         let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
         let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
     }
+
+    // Issue #5524 mutable references
+    {
+        let mut c = 42;
+        let v = vec![&mut c];
+        let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
+        let mut d = 21;
+        let v = vec![&mut d];
+        let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
+    }
 }
diff --git a/tests/ui/map_clone.rs b/tests/ui/map_clone.rs
index 2fe078b2752..8ed164f0ed5 100644
--- a/tests/ui/map_clone.rs
+++ b/tests/ui/map_clone.rs
@@ -4,6 +4,7 @@
 #![allow(clippy::clone_on_copy, clippy::redundant_clone)]
 #![allow(clippy::missing_docs_in_private_items)]
 #![allow(clippy::redundant_closure_for_method_calls)]
+#![allow(clippy::many_single_char_names)]
 
 fn main() {
     let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
@@ -33,4 +34,14 @@ fn main() {
         let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
         let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
     }
+
+    // Issue #5524 mutable references
+    {
+        let mut c = 42;
+        let v = vec![&mut c];
+        let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
+        let mut d = 21;
+        let v = vec![&mut d];
+        let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
+    }
 }
diff --git a/tests/ui/map_clone.stderr b/tests/ui/map_clone.stderr
index f67679c7b96..9eec6928e8c 100644
--- a/tests/ui/map_clone.stderr
+++ b/tests/ui/map_clone.stderr
@@ -1,5 +1,5 @@
 error: You are using an explicit closure for copying elements
-  --> $DIR/map_clone.rs:9:22
+  --> $DIR/map_clone.rs:10:22
    |
 LL |     let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![5_i8; 6].iter().copied()`
@@ -7,31 +7,31 @@ LL |     let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
    = note: `-D clippy::map-clone` implied by `-D warnings`
 
 error: You are using an explicit closure for cloning elements
-  --> $DIR/map_clone.rs:10:26
+  --> $DIR/map_clone.rs:11:26
    |
 LL |     let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`
 
 error: You are using an explicit closure for copying elements
-  --> $DIR/map_clone.rs:11:23
+  --> $DIR/map_clone.rs:12:23
    |
 LL |     let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
    |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![42, 43].iter().copied()`
 
 error: You are using an explicit closure for copying elements
-  --> $DIR/map_clone.rs:13:26
+  --> $DIR/map_clone.rs:14:26
    |
 LL |     let _: Option<u64> = Some(&16).map(|b| *b);
    |                          ^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&16).copied()`
 
 error: You are using an explicit closure for copying elements
-  --> $DIR/map_clone.rs:14:25
+  --> $DIR/map_clone.rs:15:25
    |
 LL |     let _: Option<u8> = Some(&1).map(|x| x.clone());
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&1).copied()`
 
 error: You are needlessly cloning iterator elements
-  --> $DIR/map_clone.rs:25:29
+  --> $DIR/map_clone.rs:26:29
    |
 LL |     let _ = std::env::args().map(|v| v.clone());
    |                             ^^^^^^^^^^^^^^^^^^^ help: Remove the `map` call