-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbytes_count_to_len.rs
36 lines (34 loc) · 1.25 KB
/
bytes_count_to_len.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_lang_item;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use super::BYTES_COUNT_TO_LEN;
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx hir::Expr<'_>,
count_recv: &'tcx hir::Expr<'_>,
bytes_recv: &'tcx hir::Expr<'_>,
) {
if let Some(bytes_id) = cx.typeck_results().type_dependent_def_id(count_recv.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(bytes_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_str()
&& let ty = cx.typeck_results().expr_ty(bytes_recv).peel_refs()
&& (ty.is_str() || is_type_lang_item(cx, ty, hir::LangItem::String))
{
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
BYTES_COUNT_TO_LEN,
expr.span,
"using long and hard to read `.bytes().count()`",
"consider calling `.len()` instead",
format!(
"{}.len()",
snippet_with_applicability(cx, bytes_recv.span, "..", &mut applicability)
),
applicability,
);
}
}