2021-03-25 13:29:11 -05: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 05:16:06 -05:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::ITER_COUNT;
|
|
|
|
|
2021-04-08 10:50:13 -05: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 08:30:50 -06:00
|
|
|
"slice"
|
2021-10-02 18:51:01 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::Vec) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"Vec"
|
2021-10-02 18:51:01 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::VecDeque) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"VecDeque"
|
2021-10-02 18:51:01 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::HashSet) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"HashSet"
|
2021-10-02 18:51:01 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::HashMap) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"HashMap"
|
2021-07-29 05:16:06 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"BTreeMap"
|
2021-07-29 05:16:06 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::BTreeSet) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"BTreeSet"
|
2021-07-29 05:16:06 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::LinkedList) {
|
2021-03-12 08:30:50 -06:00
|
|
|
"LinkedList"
|
2021-07-29 05:16:06 -05:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
|
2021-03-12 08:30:50 -06: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 10:50:13 -05:00
|
|
|
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
|
2021-03-12 08:30:50 -06:00
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|