]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/iter_cloned_collect.rs
Auto merge of #103828 - cassaundra:fix-format-args-span2, r=cjgillot
[rust.git] / src / tools / clippy / clippy_lints / src / methods / iter_cloned_collect.rs
1 use crate::methods::utils::derefs_to_slice;
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::LateContext;
8 use rustc_span::sym;
9
10 use super::ITER_CLONED_COLLECT;
11
12 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, method_name: &str, expr: &hir::Expr<'_>, recv: &'tcx hir::Expr<'_>) {
13     if_chain! {
14         if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Vec);
15         if let Some(slice) = derefs_to_slice(cx, recv, cx.typeck_results().expr_ty(recv));
16         if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
17
18         then {
19             span_lint_and_sugg(
20                 cx,
21                 ITER_CLONED_COLLECT,
22                 to_replace,
23                 &format!("called `iter().{method_name}().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
24                 more readable"),
25                 "try",
26                 ".to_vec()".to_string(),
27                 Applicability::MachineApplicable,
28             );
29         }
30     }
31 }