Skip to content

Commit 78d13e0

Browse files
committed
Auto merge of #4043 - rust-lang:map-clone-iter, r=matthiaskrgr
Suggest .copied() for map_clone on iterators too fixes #3958 changelog: Make `map_clone` suggest the newly-stable `Iterator::copied()` when applicable r? @mikerite @matthiaskrgr
2 parents fb61c21 + 770de14 commit 78d13e0

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

clippy_lints/src/map_clone.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
5252
if args.len() == 2;
5353
if method.ident.as_str() == "map";
5454
let ty = cx.tables.expr_ty(&args[0]);
55-
let is_option = match_type(cx, ty, &paths::OPTION);
56-
if is_option || match_trait_method(cx, e, &paths::ITERATOR);
55+
if match_type(cx, ty, &paths::OPTION) || match_trait_method(cx, e, &paths::ITERATOR);
5756
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].node;
5857
let closure_body = cx.tcx.hir().body(body_id);
5958
let closure_expr = remove_blocks(&closure_body.value);
@@ -63,16 +62,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
6362
hir::BindingAnnotation::Unannotated, .., name, None
6463
) = inner.node {
6564
if ident_eq(name, closure_expr) {
66-
// FIXME When Iterator::copied() stabilizes we can remove is_option
67-
// from here and the other lint() calls
68-
lint(cx, e.span, args[0].span, is_option);
65+
lint(cx, e.span, args[0].span, true);
6966
}
7067
},
7168
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
7269
match closure_expr.node {
7370
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
7471
if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() {
75-
lint(cx, e.span, args[0].span, is_option);
72+
lint(cx, e.span, args[0].span, true);
7673
}
7774
},
7875
hir::ExprKind::MethodCall(ref method, _, ref obj) => {
@@ -82,7 +79,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
8279
let obj_ty = cx.tables.expr_ty(&obj[0]);
8380
if let ty::Ref(_, ty, _) = obj_ty.sty {
8481
let copy = is_copy(cx, ty);
85-
lint(cx, e.span, args[0].span, is_option && copy);
82+
lint(cx, e.span, args[0].span, copy);
8683
} else {
8784
lint_needless_cloning(cx, e.span, args[0].span);
8885
}

tests/ui/map_clone.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#![allow(clippy::redundant_closure)]
77

88
fn main() {
9-
let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect();
9+
let _: Vec<i8> = vec![5_i8; 6].iter().copied().collect();
1010
let _: Vec<String> = vec![String::new()].iter().cloned().collect();
11-
let _: Vec<u32> = vec![42, 43].iter().cloned().collect();
11+
let _: Vec<u32> = vec![42, 43].iter().copied().collect();
1212
let _: Option<u64> = Some(Box::new(16)).map(|b| *b);
1313
let _: Option<u64> = Some(&16).copied();
1414
let _: Option<u8> = Some(&1).copied();

tests/ui/map_clone.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: You are using an explicit closure for cloning elements
1+
error: You are using an explicit closure for copying elements
22
--> $DIR/map_clone.rs:9:22
33
|
44
LL | let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![5_i8; 6].iter().cloned()`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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

@@ -12,11 +12,11 @@ error: You are using an explicit closure for cloning elements
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

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

2121
error: You are using an explicit closure for copying elements
2222
--> $DIR/map_clone.rs:13:26

0 commit comments

Comments
 (0)