Skip to content

Commit 9958af4

Browse files
committed
move lints() to iter_count.rs
1 parent 204b279 commit 9958af4

File tree

3 files changed

+100
-88
lines changed

3 files changed

+100
-88
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::utils::{
2+
derefs_to_slice, is_type_diagnostic_item, match_trait_method, method_chain_args, paths, snippet_with_applicability,
3+
span_lint_and_sugg,
4+
};
5+
use if_chain::if_chain;
6+
use rustc_errors::Applicability;
7+
use rustc_hir::Expr;
8+
use rustc_lint::LateContext;
9+
use rustc_span::sym;
10+
11+
use super::ITER_COUNT;
12+
13+
pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) {
14+
let mut_str = if is_mut { "_mut" } else { "" };
15+
let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() {
16+
"iter"
17+
} else if method_chain_args(expr, &["into_iter", "count"]).is_some() {
18+
"into_iter"
19+
} else {
20+
return;
21+
};
22+
if_chain! {
23+
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() {
24+
Some("slice")
25+
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
26+
Some("Vec")
27+
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) {
28+
Some("VecDeque")
29+
} else if match_trait_method(cx, expr, &paths::ITERATOR) {
30+
Some("std::iter::Iterator")
31+
} else {
32+
None
33+
};
34+
if let Some(caller_type) = caller_type;
35+
then {
36+
let mut applicability = Applicability::MachineApplicable;
37+
span_lint_and_sugg(
38+
cx,
39+
ITER_COUNT,
40+
expr.span,
41+
&format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type),
42+
"try",
43+
format!(
44+
"{}.len()",
45+
snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability),
46+
),
47+
applicability,
48+
);
49+
}
50+
}
51+
}

clippy_lints/src/methods/mod.rs

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod filter_map_identity;
44
mod implicit_clone;
55
mod inefficient_to_string;
66
mod inspect_for_each;
7+
mod iter_count;
78
mod manual_saturating_arithmetic;
89
mod option_map_unwrap_or;
910
mod unnecessary_filter_map;
@@ -32,12 +33,12 @@ use crate::consts::{constant, Constant};
3233
use crate::utils::eager_or_lazy::is_lazyness_candidate;
3334
use crate::utils::usage::mutated_variables;
3435
use crate::utils::{
35-
contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
36-
in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path,
37-
match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths,
38-
remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite,
39-
span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth,
40-
SpanlessEq,
36+
contains_return, contains_ty, derefs_to_slice, get_parent_expr, get_trait_def_id, has_iter_method, higher,
37+
implements_trait, in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
38+
match_def_path, match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args,
39+
path_to_local_id, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
40+
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs,
41+
sugg, walk_ptrs_ty_depth, SpanlessEq,
4142
};
4243

4344
declare_clippy_lint! {
@@ -1691,8 +1692,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16911692
lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1])
16921693
},
16931694
["extend", ..] => lint_extend(cx, expr, arg_lists[0]),
1694-
["count", "into_iter" | "iter"] => lint_iter_count(cx, expr, &arg_lists[1], false),
1695-
["count", "iter_mut"] => lint_iter_count(cx, expr, &arg_lists[1], true),
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),
16961697
["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false),
16971698
["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
16981699
["nth", "bytes"] => bytes_nth::lints(cx, expr, &arg_lists[1]),
@@ -2661,46 +2662,6 @@ fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_
26612662
}
26622663
}
26632664

2664-
fn lint_iter_count<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], is_mut: bool) {
2665-
let mut_str = if is_mut { "_mut" } else { "" };
2666-
let iter_method = if method_chain_args(expr, &[format!("iter{}", mut_str).as_str(), "count"]).is_some() {
2667-
"iter"
2668-
} else if method_chain_args(expr, &["into_iter", "count"]).is_some() {
2669-
"into_iter"
2670-
} else {
2671-
return;
2672-
};
2673-
if_chain! {
2674-
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() {
2675-
Some("slice")
2676-
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
2677-
Some("Vec")
2678-
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) {
2679-
Some("VecDeque")
2680-
} else if match_trait_method(cx, expr, &paths::ITERATOR) {
2681-
Some("std::iter::Iterator")
2682-
} else {
2683-
None
2684-
};
2685-
if let Some(caller_type) = caller_type;
2686-
then {
2687-
let mut applicability = Applicability::MachineApplicable;
2688-
span_lint_and_sugg(
2689-
cx,
2690-
ITER_COUNT,
2691-
expr.span,
2692-
&format!("called `.{}{}().count()` on a `{}`", iter_method, mut_str, caller_type),
2693-
"try",
2694-
format!(
2695-
"{}.len()",
2696-
snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability),
2697-
),
2698-
applicability,
2699-
);
2700-
}
2701-
}
2702-
}
2703-
27042665
fn lint_iter_nth<'tcx>(
27052666
cx: &LateContext<'tcx>,
27062667
expr: &hir::Expr<'_>,
@@ -2841,46 +2802,6 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[
28412802
}
28422803
}
28432804

2844-
fn derefs_to_slice<'tcx>(
2845-
cx: &LateContext<'tcx>,
2846-
expr: &'tcx hir::Expr<'tcx>,
2847-
ty: Ty<'tcx>,
2848-
) -> Option<&'tcx hir::Expr<'tcx>> {
2849-
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
2850-
match ty.kind() {
2851-
ty::Slice(_) => true,
2852-
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
2853-
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
2854-
ty::Array(_, size) => size
2855-
.try_eval_usize(cx.tcx, cx.param_env)
2856-
.map_or(false, |size| size < 32),
2857-
ty::Ref(_, inner, _) => may_slice(cx, inner),
2858-
_ => false,
2859-
}
2860-
}
2861-
2862-
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
2863-
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
2864-
Some(&args[0])
2865-
} else {
2866-
None
2867-
}
2868-
} else {
2869-
match ty.kind() {
2870-
ty::Slice(_) => Some(expr),
2871-
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
2872-
ty::Ref(_, inner, _) => {
2873-
if may_slice(cx, inner) {
2874-
Some(expr)
2875-
} else {
2876-
None
2877-
}
2878-
},
2879-
_ => None,
2880-
}
2881-
}
2882-
}
2883-
28842805
/// lint use of `unwrap()` for `Option`s and `Result`s
28852806
fn lint_unwrap(cx: &LateContext<'_>, expr: &hir::Expr<'_>, unwrap_args: &[hir::Expr<'_>]) {
28862807
let obj_ty = cx.typeck_results().expr_ty(&unwrap_args[0]).peel_refs();

clippy_utils/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,46 @@ pub fn is_some_ctor(cx: &LateContext<'_>, res: Res) -> bool {
18021802
false
18031803
}
18041804

1805+
pub fn derefs_to_slice<'tcx>(
1806+
cx: &LateContext<'tcx>,
1807+
expr: &'tcx hir::Expr<'tcx>,
1808+
ty: Ty<'tcx>,
1809+
) -> Option<&'tcx hir::Expr<'tcx>> {
1810+
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
1811+
match ty.kind() {
1812+
ty::Slice(_) => true,
1813+
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
1814+
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
1815+
ty::Array(_, size) => size
1816+
.try_eval_usize(cx.tcx, cx.param_env)
1817+
.map_or(false, |size| size < 32),
1818+
ty::Ref(_, inner, _) => may_slice(cx, inner),
1819+
_ => false,
1820+
}
1821+
}
1822+
1823+
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
1824+
if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
1825+
Some(&args[0])
1826+
} else {
1827+
None
1828+
}
1829+
} else {
1830+
match ty.kind() {
1831+
ty::Slice(_) => Some(expr),
1832+
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
1833+
ty::Ref(_, inner, _) => {
1834+
if may_slice(cx, inner) {
1835+
Some(expr)
1836+
} else {
1837+
None
1838+
}
1839+
},
1840+
_ => None,
1841+
}
1842+
}
1843+
}
1844+
18051845
#[cfg(test)]
18061846
mod test {
18071847
use super::{reindent_multiline, without_block_comments};

0 commit comments

Comments
 (0)