2022-04-16 08:53:22 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
|
|
|
use clippy_utils::{match_def_path, paths};
|
2022-01-31 00:16:10 +00:00
|
|
|
use if_chain::if_chain;
|
2022-04-16 08:53:22 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-01-31 00:16:10 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-04-16 08:53:22 +00:00
|
|
|
use rustc_middle::ty;
|
2022-01-31 00:16:10 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2022-04-16 08:53:22 +00:00
|
|
|
use rustc_span::sym;
|
2022-01-31 00:16:10 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// It checks for `str::bytes().count()` and suggests replacing it with
|
|
|
|
/// `str::len()`.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// `str::bytes().count()` is longer and may not be as performant as using
|
|
|
|
/// `str::len()`.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust
|
|
|
|
/// "hello".bytes().count();
|
2022-04-16 08:53:22 +00:00
|
|
|
/// String::from("hello").bytes().count();
|
2022-01-31 00:16:10 +00:00
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// "hello".len();
|
2022-04-16 08:53:22 +00:00
|
|
|
/// String::from("hello").len();
|
2022-01-31 00:16:10 +00:00
|
|
|
/// ```
|
2022-04-16 08:53:22 +00:00
|
|
|
#[clippy::version = "1.62.0"]
|
2022-01-31 00:16:10 +00:00
|
|
|
pub BYTES_COUNT_TO_LEN,
|
|
|
|
complexity,
|
2022-04-16 08:53:22 +00:00
|
|
|
"Using `bytes().count()` when `len()` performs the same functionality"
|
2022-01-31 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(BytesCountToLen => [BYTES_COUNT_TO_LEN]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for BytesCountToLen {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
|
|
|
if_chain! {
|
2022-04-16 08:53:22 +00:00
|
|
|
if let hir::ExprKind::MethodCall(_, expr_args, _) = &expr.kind;
|
|
|
|
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
|
|
|
|
if match_def_path(cx, expr_def_id, &paths::ITER_COUNT);
|
|
|
|
|
|
|
|
if let [bytes_expr] = &**expr_args;
|
|
|
|
if let hir::ExprKind::MethodCall(_, bytes_args, _) = &bytes_expr.kind;
|
|
|
|
if let Some(bytes_def_id) = cx.typeck_results().type_dependent_def_id(bytes_expr.hir_id);
|
|
|
|
if match_def_path(cx, bytes_def_id, &paths::STR_BYTES);
|
|
|
|
|
|
|
|
if let [str_expr] = &**bytes_args;
|
|
|
|
let ty = cx.typeck_results().expr_ty(str_expr).peel_refs();
|
|
|
|
|
|
|
|
if is_type_diagnostic_item(cx, ty, sym::String) || ty.kind() == &ty::Str;
|
2022-01-31 00:16:10 +00:00
|
|
|
then {
|
2022-04-16 08:53:22 +00:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
2022-01-31 00:16:10 +00:00
|
|
|
cx,
|
|
|
|
BYTES_COUNT_TO_LEN,
|
|
|
|
expr.span,
|
|
|
|
"using long and hard to read `.bytes().count()`",
|
2022-04-16 08:53:22 +00:00
|
|
|
"consider calling `.len()` instead",
|
|
|
|
format!("{}.len()", snippet_with_applicability(cx, str_expr.span, "..", &mut applicability)),
|
|
|
|
applicability
|
2022-01-31 00:16:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|