]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | use super::utils::derefs_to_slice; |
2 | use clippy_utils::diagnostics::span_lint_and_sugg; | |
3 | use clippy_utils::paths; | |
4 | use clippy_utils::source::snippet_with_applicability; | |
5 | use clippy_utils::ty::{is_type_diagnostic_item, match_type}; | |
f20569fa XL |
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 | ||
cdc7bbd5 XL |
13 | pub(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 | } |