Skip to content

Commit ad2c65b

Browse files
committed
Also suggest .copied() when .clone() is called on a Copy type
1 parent d2f7ae7 commit ad2c65b

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

clippy_lints/src/map_clone.rs

+4-3
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,
3+
in_macro, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg, is_copy
44
};
55
use if_chain::if_chain;
66
use rustc::hir;
@@ -88,8 +88,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8888
&& match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) {
8989

9090
let obj_ty = cx.tables.expr_ty(&obj[0]);
91-
if let ty::Ref(..) = obj_ty.sty {
92-
lint(cx, e.span, args[0].span, false);
91+
if let ty::Ref(_, ty, _) = obj_ty.sty {
92+
let copy = is_copy(cx, ty);
93+
lint(cx, e.span, args[0].span, copy);
9394
} else {
9495
lint_needless_cloning(cx, e.span, args[0].span);
9596
}

tests/ui/map_clone.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn main() {
1111
let _: Vec<String> = vec![String::new()].iter().cloned().collect();
1212
let _: Vec<u32> = vec![42, 43].iter().copied().collect();
1313
let _: Option<u64> = Some(Box::new(16)).map(|b| *b);
14+
let _: Vec<u8> = vec![1; 6].iter().copied().collect();
1415

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

tests/ui/map_clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn main() {
1111
let _: Vec<String> = vec![String::new()].iter().map(|x| x.clone()).collect();
1212
let _: Vec<u32> = vec![42, 43].iter().map(|&x| x).collect();
1313
let _: Option<u64> = Some(Box::new(16)).map(|b| *b);
14+
let _: Vec<u8> = vec![1; 6].iter().map(|x| x.clone()).collect();
1415

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

tests/ui/map_clone.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ error: You are using an explicit closure for copying elements
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

21+
error: You are using an explicit closure for copying elements
22+
--> $DIR/map_clone.rs:14:22
23+
|
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()`
26+
2127
error: You are needlessly cloning iterator elements
22-
--> $DIR/map_clone.rs:24:29
28+
--> $DIR/map_clone.rs:25:29
2329
|
2430
LL | let _ = std::env::args().map(|v| v.clone());
2531
| ^^^^^^^^^^^^^^^^^^^ help: Remove the map call
2632

27-
error: aborting due to 4 previous errors
33+
error: aborting due to 5 previous errors
2834

0 commit comments

Comments
 (0)