Skip to content

Commit

Permalink
receive iter method name as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Feb 27, 2021
1 parent 8bae279 commit 77907e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
16 changes: 3 additions & 13 deletions clippy_lints/src/methods/iter_count.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
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;
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"
Expand Down Expand Up @@ -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()",
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down

0 comments on commit 77907e6

Please sign in to comment.