Skip to content

Commit 45ee914

Browse files
committed
move iter_cloned_collect to its own module
1 parent 35147d4 commit 45ee914

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::methods::derefs_to_slice;
2+
use crate::utils::{is_type_diagnostic_item, span_lint_and_sugg};
3+
use if_chain::if_chain;
4+
use rustc_errors::Applicability;
5+
use rustc_hir as hir;
6+
use rustc_lint::LateContext;
7+
use rustc_span::sym;
8+
9+
use super::ITER_CLONED_COLLECT;
10+
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, iter_args: &'tcx [hir::Expr<'_>]) {
12+
if_chain! {
13+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type);
14+
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0]));
15+
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
16+
17+
then {
18+
span_lint_and_sugg(
19+
cx,
20+
ITER_CLONED_COLLECT,
21+
to_replace,
22+
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
23+
more readable",
24+
"try",
25+
".to_vec()".to_string(),
26+
Applicability::MachineApplicable,
27+
);
28+
}
29+
}
30+
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod get_unwrap;
88
mod implicit_clone;
99
mod inefficient_to_string;
1010
mod inspect_for_each;
11+
mod iter_cloned_collect;
1112
mod iter_count;
1213
mod manual_saturating_arithmetic;
1314
mod ok_expect;
@@ -1711,7 +1712,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17111712
["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
17121713
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
17131714
["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
1714-
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
1715+
["collect", "cloned"] => iter_cloned_collect::check(cx, expr, arg_lists[1]),
17151716
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
17161717
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
17171718
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0], method_spans[0]),
@@ -2494,27 +2495,6 @@ fn lint_extend(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>
24942495
}
24952496
}
24962497

2497-
fn lint_iter_cloned_collect<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, iter_args: &'tcx [hir::Expr<'_>]) {
2498-
if_chain! {
2499-
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type);
2500-
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0]));
2501-
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
2502-
2503-
then {
2504-
span_lint_and_sugg(
2505-
cx,
2506-
ITER_CLONED_COLLECT,
2507-
to_replace,
2508-
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
2509-
more readable",
2510-
"try",
2511-
".to_vec()".to_string(),
2512-
Applicability::MachineApplicable,
2513-
);
2514-
}
2515-
}
2516-
}
2517-
25182498
fn lint_unnecessary_fold(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir::Expr<'_>], fold_span: Span) {
25192499
fn check_fold_with_op(
25202500
cx: &LateContext<'_>,

0 commit comments

Comments
 (0)