move iter_cloned_collect to its own module
This commit is contained in:
parent
35147d4cf3
commit
45ee914df0
2 changed files with 32 additions and 22 deletions
30
clippy_lints/src/methods/iter_cloned_collect.rs
Normal file
30
clippy_lints/src/methods/iter_cloned_collect.rs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
use crate::methods::derefs_to_slice;
|
||||||
|
use crate::utils::{is_type_diagnostic_item, span_lint_and_sugg};
|
||||||
|
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;
|
||||||
|
|
||||||
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, iter_args: &'tcx [hir::Expr<'_>]) {
|
||||||
|
if_chain! {
|
||||||
|
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type);
|
||||||
|
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0]));
|
||||||
|
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
|
||||||
|
|
||||||
|
then {
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
ITER_CLONED_COLLECT,
|
||||||
|
to_replace,
|
||||||
|
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
|
||||||
|
more readable",
|
||||||
|
"try",
|
||||||
|
".to_vec()".to_string(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ mod get_unwrap;
|
||||||
mod implicit_clone;
|
mod implicit_clone;
|
||||||
mod inefficient_to_string;
|
mod inefficient_to_string;
|
||||||
mod inspect_for_each;
|
mod inspect_for_each;
|
||||||
|
mod iter_cloned_collect;
|
||||||
mod iter_count;
|
mod iter_count;
|
||||||
mod manual_saturating_arithmetic;
|
mod manual_saturating_arithmetic;
|
||||||
mod ok_expect;
|
mod ok_expect;
|
||||||
|
@ -1711,7 +1712,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
|
||||||
["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
|
["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
|
||||||
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
|
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
|
||||||
["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
|
["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
|
||||||
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
|
["collect", "cloned"] => iter_cloned_collect::check(cx, expr, arg_lists[1]),
|
||||||
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
|
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
|
||||||
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
|
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
|
||||||
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0], method_spans[0]),
|
["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<'_>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lint_iter_cloned_collect<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, iter_args: &'tcx [hir::Expr<'_>]) {
|
|
||||||
if_chain! {
|
|
||||||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type);
|
|
||||||
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0]));
|
|
||||||
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
|
|
||||||
|
|
||||||
then {
|
|
||||||
span_lint_and_sugg(
|
|
||||||
cx,
|
|
||||||
ITER_CLONED_COLLECT,
|
|
||||||
to_replace,
|
|
||||||
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
|
|
||||||
more readable",
|
|
||||||
"try",
|
|
||||||
".to_vec()".to_string(),
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lint_unnecessary_fold(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir::Expr<'_>], fold_span: Span) {
|
fn lint_unnecessary_fold(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir::Expr<'_>], fold_span: Span) {
|
||||||
fn check_fold_with_op(
|
fn check_fold_with_op(
|
||||||
cx: &LateContext<'_>,
|
cx: &LateContext<'_>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue