Skip to content

Commit a76bfd4

Browse files
committed
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
2 parents 77c23b7 + 806d973 commit a76bfd4

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

clippy_lints/src/map_clone.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast::ast::Ident;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
99
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_middle::mir::Mutability;
1011
use rustc_middle::ty;
1112
use rustc_session::{declare_lint_pass, declare_tool_lint};
1213
use rustc_span::source_map::Span;
@@ -58,7 +59,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
5859
let closure_expr = remove_blocks(&closure_body.value);
5960
then {
6061
match closure_body.params[0].pat.kind {
61-
hir::PatKind::Ref(ref inner, _) => if let hir::PatKind::Binding(
62+
hir::PatKind::Ref(ref inner, hir::Mutability::Not) => if let hir::PatKind::Binding(
6263
hir::BindingAnnotation::Unannotated, .., name, None
6364
) = inner.kind {
6465
if ident_eq(name, closure_expr) {
@@ -69,7 +70,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
6970
match closure_expr.kind {
7071
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
7172
if ident_eq(name, inner) {
72-
if let ty::Ref(..) = cx.tables.expr_ty(inner).kind {
73+
if let ty::Ref(.., Mutability::Not) = cx.tables.expr_ty(inner).kind {
7374
lint(cx, e.span, args[0].span, true);
7475
}
7576
}

tests/ui/map_clone.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(clippy::clone_on_copy, clippy::redundant_clone)]
55
#![allow(clippy::missing_docs_in_private_items)]
66
#![allow(clippy::redundant_closure_for_method_calls)]
7+
#![allow(clippy::many_single_char_names)]
78

89
fn main() {
910
let _: Vec<i8> = vec![5_i8; 6].iter().copied().collect();
@@ -33,4 +34,14 @@ fn main() {
3334
let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
3435
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
3536
}
37+
38+
// Issue #5524 mutable references
39+
{
40+
let mut c = 42;
41+
let v = vec![&mut c];
42+
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
43+
let mut d = 21;
44+
let v = vec![&mut d];
45+
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
46+
}
3647
}

tests/ui/map_clone.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(clippy::clone_on_copy, clippy::redundant_clone)]
55
#![allow(clippy::missing_docs_in_private_items)]
66
#![allow(clippy::redundant_closure_for_method_calls)]
7+
#![allow(clippy::many_single_char_names)]
78

89
fn main() {
910
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
@@ -33,4 +34,14 @@ fn main() {
3334
let v: Vec<Rc<u32>> = vec![Rc::new(0_u32)];
3435
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
3536
}
37+
38+
// Issue #5524 mutable references
39+
{
40+
let mut c = 42;
41+
let v = vec![&mut c];
42+
let _: Vec<u32> = v.into_iter().map(|x| *x).collect();
43+
let mut d = 21;
44+
let v = vec![&mut d];
45+
let _: Vec<u32> = v.into_iter().map(|&mut x| x).collect();
46+
}
3647
}

tests/ui/map_clone.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
error: You are using an explicit closure for copying elements
2-
--> $DIR/map_clone.rs:9:22
2+
--> $DIR/map_clone.rs:10:22
33
|
44
LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![5_i8; 6].iter().copied()`
66
|
77
= note: `-D clippy::map-clone` implied by `-D warnings`
88

99
error: You are using an explicit closure for cloning elements
10-
--> $DIR/map_clone.rs:10:26
10+
--> $DIR/map_clone.rs:11:26
1111
|
1212
LL | let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![String::new()].iter().cloned()`
1414

1515
error: You are using an explicit closure for copying elements
16-
--> $DIR/map_clone.rs:11:23
16+
--> $DIR/map_clone.rs:12:23
1717
|
1818
LL | let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![42, 43].iter().copied()`
2020

2121
error: You are using an explicit closure for copying elements
22-
--> $DIR/map_clone.rs:13:26
22+
--> $DIR/map_clone.rs:14:26
2323
|
2424
LL | let _: Option<u64> = Some(&16).map(|b| *b);
2525
| ^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&16).copied()`
2626

2727
error: You are using an explicit closure for copying elements
28-
--> $DIR/map_clone.rs:14:25
28+
--> $DIR/map_clone.rs:15:25
2929
|
3030
LL | let _: Option<u8> = Some(&1).map(|x| x.clone());
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&1).copied()`
3232

3333
error: You are needlessly cloning iterator elements
34-
--> $DIR/map_clone.rs:25:29
34+
--> $DIR/map_clone.rs:26:29
3535
|
3636
LL | let _ = std::env::args().map(|v| v.clone());
3737
| ^^^^^^^^^^^^^^^^^^^ help: Remove the `map` call

0 commit comments

Comments
 (0)