2021-03-25 13:29:11 -05:00
|
|
|
use crate::methods::utils::derefs_to_slice;
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-03-12 08:30:50 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::ITER_CLONED_COLLECT;
|
|
|
|
|
2021-11-20 03:53:15 -06:00
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, method_name: &str, expr: &hir::Expr<'_>, recv: &'tcx hir::Expr<'_>) {
|
2021-03-12 08:30:50 -06:00
|
|
|
if_chain! {
|
2021-10-02 18:51:01 -05:00
|
|
|
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Vec);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(slice) = derefs_to_slice(cx, recv, cx.typeck_results().expr_ty(recv));
|
2021-03-12 08:30:50 -06:00
|
|
|
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
|
|
|
|
|
|
|
|
then {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
ITER_CLONED_COLLECT,
|
|
|
|
to_replace,
|
2021-11-20 03:53:15 -06:00
|
|
|
&format!("called `iter().{}().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
|
|
|
|
more readable", method_name),
|
2021-03-12 08:30:50 -06:00
|
|
|
"try",
|
|
|
|
".to_vec()".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|