receive iter method name as an argument
This commit is contained in:
parent
8bae279706
commit
77907e6dab
2 changed files with 6 additions and 15 deletions
|
|
@ -1,9 +1,7 @@
|
|||
use crate::utils::{
|
||||
derefs_to_slice, is_type_diagnostic_item, match_type, method_chain_args, paths, snippet_with_applicability,
|
||||
span_lint_and_sugg,
|
||||
derefs_to_slice, is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg,
|
||||
};
|
||||
|
||||
use if_chain::if_chain;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::Expr;
|
||||
use rustc_lint::LateContext;
|
||||
|
|
@ -11,15 +9,7 @@ use rustc_span::sym;
|
|||
|
||||
use super::ITER_COUNT;
|
||||
|
||||
pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) {
|
||||
let mut_str = if is_mut { "_mut" } else { "" };
|
||||
let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() {
|
||||
"iter"
|
||||
} else if method_chain_args(expr, &["into_iter", "count"]).is_some() {
|
||||
"into_iter"
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], iter_method: &str) {
|
||||
let ty = cx.typeck_results().expr_ty(&iter_args[0]);
|
||||
let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() {
|
||||
"slice"
|
||||
|
|
@ -47,7 +37,7 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'
|
|||
cx,
|
||||
ITER_COUNT,
|
||||
expr.span,
|
||||
&format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type),
|
||||
&format!("called `.{}().count()` on a `{}`", iter_method, caller_type),
|
||||
"try",
|
||||
format!(
|
||||
"{}.len()",
|
||||
|
|
|
|||
|
|
@ -1692,8 +1692,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
|
|||
lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1])
|
||||
},
|
||||
["extend", ..] => lint_extend(cx, expr, arg_lists[0]),
|
||||
["count", "into_iter" | "iter"] => iter_count::lints(cx, expr, &arg_lists[1], false),
|
||||
["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], true),
|
||||
["count", "into_iter"] => iter_count::lints(cx, expr, &arg_lists[1], "into_iter"),
|
||||
["count", "iter"] => iter_count::lints(cx, expr, &arg_lists[1], "iter"),
|
||||
["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], "iter_mut"),
|
||||
["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false),
|
||||
["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
|
||||
["nth", "bytes"] => bytes_nth::lints(cx, expr, &arg_lists[1]),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue