Skip to content

Commit 4e84dd1

Browse files
committed
Auto merge of rust-lang#8006 - togami2864:generalize-copied, r=camsteffen
apply iter_cloned_collect to collect() using copied() fix: rust-lang#6703 changelog: apply `iter_cloned_collect` to `collect()` using`copied()`
2 parents 3720735 + f51bbc7 commit 4e84dd1

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

clippy_lints/src/methods/iter_cloned_collect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_span::sym;
99

1010
use super::ITER_CLONED_COLLECT;
1111

12-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, recv: &'tcx hir::Expr<'_>) {
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, method_name: &str, expr: &hir::Expr<'_>, recv: &'tcx hir::Expr<'_>) {
1313
if_chain! {
1414
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Vec);
1515
if let Some(slice) = derefs_to_slice(cx, recv, cx.typeck_results().expr_ty(recv));
@@ -20,8 +20,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, recv: &'
2020
cx,
2121
ITER_CLONED_COLLECT,
2222
to_replace,
23-
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
24-
more readable",
23+
&format!("called `iter().{}().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
24+
more readable", method_name),
2525
"try",
2626
".to_vec()".to_string(),
2727
Applicability::MachineApplicable,

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
22042204
("assume_init", []) => uninit_assumed_init::check(cx, expr, recv),
22052205
("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, msrv),
22062206
("collect", []) => match method_call!(recv) {
2207-
Some(("cloned", [recv2], _)) => iter_cloned_collect::check(cx, expr, recv2),
2207+
Some((name @ ("cloned" | "copied"), [recv2], _)) => {
2208+
iter_cloned_collect::check(cx, name, expr, recv2);
2209+
},
22082210
Some(("map", [m_recv, m_arg], _)) => {
22092211
map_collect_result_unit::check(cx, expr, m_recv, m_arg, recv);
22102212
},

tests/ui/iter_cloned_collect.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ fn main() {
2323
// Issue #6808
2424
let arr: [u8; 64] = [0; 64];
2525
let _: Vec<_> = arr.to_vec();
26+
27+
// Issue #6703
28+
let _: Vec<isize> = v.to_vec();
2629
}

tests/ui/iter_cloned_collect.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ fn main() {
2626
// Issue #6808
2727
let arr: [u8; 64] = [0; 64];
2828
let _: Vec<_> = arr.iter().cloned().collect();
29+
30+
// Issue #6703
31+
let _: Vec<isize> = v.iter().copied().collect();
2932
}

tests/ui/iter_cloned_collect.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ error: called `iter().cloned().collect()` on a slice to create a `Vec`. Calling
2828
LL | let _: Vec<_> = arr.iter().cloned().collect();
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`
3030

31-
error: aborting due to 4 previous errors
31+
error: called `iter().copied().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
32+
--> $DIR/iter_cloned_collect.rs:31:26
33+
|
34+
LL | let _: Vec<isize> = v.iter().copied().collect();
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`
36+
37+
error: aborting due to 5 previous errors
3238

0 commit comments

Comments
 (0)