Skip to content

Commit e9cde41

Browse files
committed
Only suggest .copied() for Option right now
1 parent ad2c65b commit e9cde41

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

clippy_lints/src/map_clone.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::paths;
22
use crate::utils::{
3-
in_macro, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg, is_copy
3+
in_macro, is_copy, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc::hir;
@@ -63,7 +63,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
6363
if args.len() == 2;
6464
if method.ident.as_str() == "map";
6565
let ty = cx.tables.expr_ty(&args[0]);
66-
if match_type(cx, ty, &paths::OPTION) || match_trait_method(cx, e, &paths::ITERATOR);
66+
let is_option = match_type(cx, ty, &paths::OPTION);
67+
if is_option || match_trait_method(cx, e, &paths::ITERATOR);
6768
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].node;
6869
let closure_body = cx.tcx.hir().body(body_id);
6970
let closure_expr = remove_blocks(&closure_body.value);
@@ -73,14 +74,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
7374
hir::BindingAnnotation::Unannotated, .., name, None
7475
) = inner.node {
7576
if ident_eq(name, closure_expr) {
76-
lint(cx, e.span, args[0].span, true);
77+
// FIXME When Iterator::copied() stabilizes we can remove is_option
78+
// from here and the other lint() calls
79+
lint(cx, e.span, args[0].span, is_option);
7780
}
7881
},
7982
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
8083
match closure_expr.node {
8184
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => {
8285
if ident_eq(name, inner) && !cx.tables.expr_ty(inner).is_box() {
83-
lint(cx, e.span, args[0].span, true);
86+
lint(cx, e.span, args[0].span, is_option);
8487
}
8588
},
8689
hir::ExprKind::MethodCall(ref method, _, ref obj) => {
@@ -90,7 +93,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
9093
let obj_ty = cx.tables.expr_ty(&obj[0]);
9194
if let ty::Ref(_, ty, _) = obj_ty.sty {
9295
let copy = is_copy(cx, ty);
93-
lint(cx, e.span, args[0].span, copy);
96+
lint(cx, e.span, args[0].span, is_option && copy);
9497
} else {
9598
lint_needless_cloning(cx, e.span, args[0].span);
9699
}

tests/ui/map_clone.fixed

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

98
fn main() {
10-
let _: Vec<i8> = vec![5_i8; 6].iter().copied().collect();
9+
let _: Vec<i8> = vec![5_i8; 6].iter().cloned().collect();
1110
let _: Vec<String> = vec![String::new()].iter().cloned().collect();
12-
let _: Vec<u32> = vec![42, 43].iter().copied().collect();
11+
let _: Vec<u32> = vec![42, 43].iter().cloned().collect();
1312
let _: Option<u64> = Some(Box::new(16)).map(|b| *b);
14-
let _: Vec<u8> = vec![1; 6].iter().copied().collect();
13+
let _: Option<u64> = Some(&16).copied();
14+
let _: Option<u8> = Some(&1).copied();
1515

1616
// Don't lint these
1717
let v = vec![5_i8; 6];

tests/ui/map_clone.rs

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

98
fn main() {
109
let _: Vec<i8> = vec![5_i8; 6].iter().map(|x| *x).collect();
1110
let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
1211
let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
1312
let _: Option<u64> = Some(Box::new(16)).map(|b| *b);
14-
let _: Vec<u8> = vec![1; 6].iter().map(|x| x.clone()).collect();
13+
let _: Option<u64> = Some(&16).map(|b| *b);
14+
let _: Option<u8> = Some(&1).map(|x| x.clone());
1515

1616
// Don't lint these
1717
let v = vec![5_i8; 6];

tests/ui/map_clone.stderr

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
error: You are using an explicit closure for copying elements
2-
--> $DIR/map_clone.rs:10:22
1+
error: You are using an explicit closure for cloning elements
2+
--> $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 `copied` method: `vec![5_i8; 6].iter().copied()`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![5_i8; 6].iter().cloned()`
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:11:26
10+
--> $DIR/map_clone.rs:10: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 copying elements
16-
--> $DIR/map_clone.rs:12:23
15+
error: You are using an explicit closure for cloning elements
16+
--> $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 `copied` method: `vec![42, 43].iter().copied()`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `vec![42, 43].iter().cloned()`
20+
21+
error: You are using an explicit closure for copying elements
22+
--> $DIR/map_clone.rs:13:26
23+
|
24+
LL | let _: Option<u64> = Some(&16).map(|b| *b);
25+
| ^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&16).copied()`
2026

2127
error: You are using an explicit closure for copying elements
22-
--> $DIR/map_clone.rs:14:22
28+
--> $DIR/map_clone.rs:14:25
2329
|
24-
LL | let _: Vec<u8> = vec![1; 6].iter().map(|x| x.clone()).collect();
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `vec![1; 6].iter().copied()`
30+
LL | let _: Option<u8> = Some(&1).map(|x| x.clone());
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `Some(&1).copied()`
2632

2733
error: You are needlessly cloning iterator elements
2834
--> $DIR/map_clone.rs:25:29
2935
|
3036
LL | let _ = std::env::args().map(|v| v.clone());
3137
| ^^^^^^^^^^^^^^^^^^^ help: Remove the map call
3238

33-
error: aborting due to 5 previous errors
39+
error: aborting due to 6 previous errors
3440

0 commit comments

Comments
 (0)