2021-03-25 18:29:11 +00:00
|
|
|
use super::utils::derefs_to_slice;
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-07-29 10:16:06 +00:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-03-12 14:30:50 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::ITER_COUNT;
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, iter_method: &str) {
|
|
|
|
let ty = cx.typeck_results().expr_ty(recv);
|
|
|
|
let caller_type = if derefs_to_slice(cx, recv, ty).is_some() {
|
2021-03-12 14:30:50 +00:00
|
|
|
"slice"
|
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::vec_type) {
|
|
|
|
"Vec"
|
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::vecdeque_type) {
|
|
|
|
"VecDeque"
|
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::hashset_type) {
|
|
|
|
"HashSet"
|
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
|
|
|
|
"HashMap"
|
2021-07-29 10:16:06 +00:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) {
|
2021-03-12 14:30:50 +00:00
|
|
|
"BTreeMap"
|
2021-07-29 10:16:06 +00:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::BTreeSet) {
|
2021-03-12 14:30:50 +00:00
|
|
|
"BTreeSet"
|
2021-07-29 10:16:06 +00:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::LinkedList) {
|
2021-03-12 14:30:50 +00:00
|
|
|
"LinkedList"
|
2021-07-29 10:16:06 +00:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
|
2021-03-12 14:30:50 +00:00
|
|
|
"BinaryHeap"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
ITER_COUNT,
|
|
|
|
expr.span,
|
|
|
|
&format!("called `.{}().count()` on a `{}`", iter_method, caller_type),
|
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"{}.len()",
|
2021-04-08 15:50:13 +00:00
|
|
|
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
|
2021-03-12 14:30:50 +00:00
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|