Skip to content

Commit 77907e6

Browse files
committed
receive iter method name as an argument
1 parent 8bae279 commit 77907e6

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

clippy_lints/src/methods/iter_count.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
use crate::utils::{
2-
derefs_to_slice, is_type_diagnostic_item, match_type, method_chain_args, paths, snippet_with_applicability,
3-
span_lint_and_sugg,
2+
derefs_to_slice, is_type_diagnostic_item, match_type, paths, snippet_with_applicability, span_lint_and_sugg,
43
};
54

6-
use if_chain::if_chain;
75
use rustc_errors::Applicability;
86
use rustc_hir::Expr;
97
use rustc_lint::LateContext;
108
use rustc_span::sym;
119

1210
use super::ITER_COUNT;
1311

14-
pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) {
15-
let mut_str = if is_mut { "_mut" } else { "" };
16-
let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() {
17-
"iter"
18-
} else if method_chain_args(expr, &["into_iter", "count"]).is_some() {
19-
"into_iter"
20-
} else {
21-
return;
22-
};
12+
pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], iter_method: &str) {
2313
let ty = cx.typeck_results().expr_ty(&iter_args[0]);
2414
let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() {
2515
"slice"
@@ -47,7 +37,7 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'
4737
cx,
4838
ITER_COUNT,
4939
expr.span,
50-
&format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type),
40+
&format!("called `.{}().count()` on a `{}`", iter_method, caller_type),
5141
"try",
5242
format!(
5343
"{}.len()",

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,8 +1692,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16921692
lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1])
16931693
},
16941694
["extend", ..] => lint_extend(cx, expr, arg_lists[0]),
1695-
["count", "into_iter" | "iter"] => iter_count::lints(cx, expr, &arg_lists[1], false),
1696-
["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], true),
1695+
["count", "into_iter"] => iter_count::lints(cx, expr, &arg_lists[1], "into_iter"),
1696+
["count", "iter"] => iter_count::lints(cx, expr, &arg_lists[1], "iter"),
1697+
["count", "iter_mut"] => iter_count::lints(cx, expr, &arg_lists[1], "iter_mut"),
16971698
["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false),
16981699
["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
16991700
["nth", "bytes"] => bytes_nth::lints(cx, expr, &arg_lists[1]),

0 commit comments

Comments
 (0)