]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/iter_cloned_collect.rs
Auto merge of #82680 - jturner314:div_euclid-docs, r=JohnTitor
[rust.git] / src / tools / clippy / clippy_lints / src / methods / iter_cloned_collect.rs
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 }