2021-02-26 02:07:00 -06:00
|
|
|
use crate::methods::derefs_to_slice;
|
2021-03-15 19:55:45 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-16 11:06:34 -05:00
|
|
|
use clippy_utils::paths;
|
2021-03-14 18:17:44 -05:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-03-13 17:01:03 -06:00
|
|
|
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
|
2021-02-25 11:18:52 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::ITER_COUNT;
|
|
|
|
|
2021-03-02 10:16:16 -06:00
|
|
|
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'tcx [Expr<'tcx>], iter_method: &str) {
|
2021-02-25 13:11:01 -06:00
|
|
|
let ty = cx.typeck_results().expr_ty(&iter_args[0]);
|
2021-02-26 00:49:03 -06:00
|
|
|
let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() {
|
|
|
|
"slice"
|
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::vec_type) {
|
|
|
|
"Vec"
|
2021-03-11 03:57:49 -06:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::vecdeque_type) {
|
2021-02-26 00:49:03 -06:00
|
|
|
"VecDeque"
|
2021-03-11 03:57:49 -06:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::hashset_type) {
|
2021-02-26 00:49:03 -06:00
|
|
|
"HashSet"
|
2021-03-11 03:57:49 -06:00
|
|
|
} else if is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
|
2021-02-26 00:49:03 -06:00
|
|
|
"HashMap"
|
|
|
|
} else if match_type(cx, ty, &paths::BTREEMAP) {
|
|
|
|
"BTreeMap"
|
|
|
|
} else if match_type(cx, ty, &paths::BTREESET) {
|
|
|
|
"BTreeSet"
|
|
|
|
} else if match_type(cx, ty, &paths::LINKED_LIST) {
|
|
|
|
"LinkedList"
|
|
|
|
} else if match_type(cx, ty, &paths::BINARY_HEAP) {
|
|
|
|
"BinaryHeap"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
ITER_COUNT,
|
|
|
|
expr.span,
|
2021-02-26 01:21:43 -06:00
|
|
|
&format!("called `.{}().count()` on a `{}`", iter_method, caller_type),
|
2021-02-26 00:49:03 -06:00
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"{}.len()",
|
|
|
|
snippet_with_applicability(cx, iter_args[0].span, "..", &mut applicability),
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
2021-02-25 11:18:52 -06:00
|
|
|
}
|