]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/iter_count.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / iter_count.rs
CommitLineData
cdc7bbd5
XL
1use super::utils::derefs_to_slice;
2use clippy_utils::diagnostics::span_lint_and_sugg;
3use clippy_utils::paths;
4use clippy_utils::source::snippet_with_applicability;
5use clippy_utils::ty::{is_type_diagnostic_item, match_type};
f20569fa
XL
6use rustc_errors::Applicability;
7use rustc_hir::Expr;
8use rustc_lint::LateContext;
9use rustc_span::sym;
10
11use super::ITER_COUNT;
12
cdc7bbd5
XL
13pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, iter_method: &str) {
14 let ty = cx.typeck_results().expr_ty(recv);
15 let caller_type = if derefs_to_slice(cx, recv, ty).is_some() {
f20569fa
XL
16 "slice"
17 } else if is_type_diagnostic_item(cx, ty, sym::vec_type) {
18 "Vec"
19 } else if is_type_diagnostic_item(cx, ty, sym::vecdeque_type) {
20 "VecDeque"
21 } else if is_type_diagnostic_item(cx, ty, sym::hashset_type) {
22 "HashSet"
23 } else if is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
24 "HashMap"
25 } else if match_type(cx, ty, &paths::BTREEMAP) {
26 "BTreeMap"
27 } else if match_type(cx, ty, &paths::BTREESET) {
28 "BTreeSet"
29 } else if match_type(cx, ty, &paths::LINKED_LIST) {
30 "LinkedList"
31 } else if match_type(cx, ty, &paths::BINARY_HEAP) {
32 "BinaryHeap"
33 } else {
34 return;
35 };
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, caller_type),
42 "try",
43 format!(
44 "{}.len()",
cdc7bbd5 45 snippet_with_applicability(cx, recv.span, "..", &mut applicability),
f20569fa
XL
46 ),
47 applicability,
48 );
49}