Skip to content

Commit d2f7ae7

Browse files
committed
Suggest .copied() instead of .cloned() in map_clone when dealing with references
1 parent fbb3a47 commit d2f7ae7

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

clippy_lints/src/map_clone.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
7373
hir::BindingAnnotation::Unannotated, .., name, None
7474
) = inner.node {
7575
if ident_eq(name, closure_expr) {
76-
lint(cx, e.span, args[0].span);
76+
lint(cx, e.span, args[0].span, true);
7777
}
7878
},
7979
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
8080
match closure_expr.node {
8181
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
8282
if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() {
83-
lint(cx, e.span, args[0].span);
83+
lint(cx, e.span, args[0].span, true);
8484
}
8585
},
8686
hir::ExprKind::MethodCall(ref method, _, ref obj) => {
@@ -89,7 +89,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8989

9090
let obj_ty = cx.tables.expr_ty(&obj[0]);
9191
if let ty::Ref(..) = obj_ty.sty {
92-
lint(cx, e.span, args[0].span);
92+
lint(cx, e.span, args[0].span, false);
9393
} else {
9494
lint_needless_cloning(cx, e.span, args[0].span);
9595
}
@@ -125,18 +125,33 @@ fn lint_needless_cloning(cx: &LateContext<'_, '_>, root: Span, receiver: Span) {
125125
)
126126
}
127127

128-
fn lint(cx: &LateContext<'_, '_>, replace: Span, root: Span) {
128+
fn lint(cx: &LateContext<'_, '_>, replace: Span, root: Span, copied: bool) {
129129
let mut applicability = Applicability::MachineApplicable;
130-
span_lint_and_sugg(
131-
cx,
132-
MAP_CLONE,
133-
replace,
134-
"You are using an explicit closure for cloning elements",
135-
"Consider calling the dedicated `cloned` method",
136-
format!(
137-
"{}.cloned()",
138-
snippet_with_applicability(cx, root, "..", &mut applicability)
139-
),
140-
applicability,
141-
)
130+
if copied {
131+
span_lint_and_sugg(
132+
cx,
133+
MAP_CLONE,
134+
replace,
135+
"You are using an explicit closure for copying elements",
136+
"Consider calling the dedicated `copied` method",
137+
format!(
138+
"{}.copied()",
139+
snippet_with_applicability(cx, root, "..", &mut applicability)
140+
),
141+
applicability,
142+
)
143+
} else {
144+
span_lint_and_sugg(
145+
cx,
146+
MAP_CLONE,
147+
replace,
148+
"You are using an explicit closure for cloning elements",
149+
"Consider calling the dedicated `cloned` method",
150+
format!(
151+
"{}.cloned()",
152+
snippet_with_applicability(cx, root, "..", &mut applicability)
153+
),
154+
applicability,
155+
)
156+
}
142157
}

tests/ui/map_clone.fixed

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
#![allow(clippy::clone_on_copy)]
55
#![allow(clippy::missing_docs_in_private_items)]
66
#![allow(clippy::redundant_closure)]
7+
#![feature(iter_copied)]
78

89
fn main() {
9-
let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect();
10+
let _: Vec<i8> = vec![5_i8; 6].iter().copied().collect();
1011
let _: Vec<String> = vec![String::new()].iter().cloned().collect();
11-
let _: Vec<u32> = vec![42, 43].iter().cloned().collect();
12+
let _: Vec<u32> = vec![42, 43].iter().copied().collect();
1213
let _: Option<u64> = Some(Box::new(16)).map(|b| *b);
1314

1415
// Don't lint these

tests/ui/map_clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(clippy::clone_on_copy)]
55
#![allow(clippy::missing_docs_in_private_items)]
66
#![allow(clippy::redundant_closure)]
7+
#![feature(iter_copied)]
78

89
fn main() {
910
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();

tests/ui/map_clone.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
error: You are using an explicit closure for cloning elements
2-
--> $DIR/map_clone.rs:9:22
1+
error: You are using an explicit closure for copying elements
2+
--> $DIR/map_clone.rs:10: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

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

15-
error: You are using an explicit closure for cloning elements
16-
--> $DIR/map_clone.rs:11:23
15+
error: You are using an explicit closure for copying elements
16+
--> $DIR/map_clone.rs:12: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 needlessly cloning iterator elements
22-
--> $DIR/map_clone.rs:23:29
22+
--> $DIR/map_clone.rs:24:29
2323
|
2424
LL | let _ = std::env::args().map(|v| v.clone());
2525
| ^^^^^^^^^^^^^^^^^^^ help: Remove the map call

0 commit comments

Comments
 (0)