2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::match_type;
|
2021-09-08 09:31:47 -05:00
|
|
|
use clippy_utils::visitors::is_local_used;
|
2021-12-17 06:40:22 -06:00
|
|
|
use clippy_utils::{path_to_local_id, paths, peel_blocks, peel_ref_operators, strip_pat_refs};
|
2018-11-20 07:06:29 -06:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-07-01 11:17:38 -05:00
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind, PatKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-12-12 08:32:45 -06:00
|
|
|
use rustc_middle::ty::{self, UintTy};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-11-05 07:29:48 -06:00
|
|
|
use rustc_span::sym;
|
2017-08-22 16:45:08 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for naive byte counts
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The [`bytecount`](https://crates.io/crates/bytecount)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// crate has methods to count your bytes faster, especially for large slices.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// If you have predominantly small slices, the
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `bytecount::count(..)` method may actually be slower. However, if you can
|
|
|
|
/// ensure that less than 2³²-1 matches arise, the `naive_count_32(..)` can be
|
|
|
|
/// faster in those cases.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2019-08-03 01:01:27 -05:00
|
|
|
/// # let vec = vec![1_u8];
|
|
|
|
/// &vec.iter().filter(|x| **x == 0u8).count(); // use bytecount::count instead
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2017-08-22 16:45:08 -05:00
|
|
|
pub NAIVE_BYTECOUNT,
|
2021-03-12 08:30:50 -06:00
|
|
|
pedantic,
|
2017-08-22 16:45:08 -05:00
|
|
|
"use of naive `<slice>.filter(|&x| x == y).count()` to count byte values"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(ByteCount => [NAIVE_BYTECOUNT]);
|
2017-08-22 16:45:08 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for ByteCount {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2021-07-01 11:17:38 -05:00
|
|
|
if let ExprKind::MethodCall(count, _, [count_recv], _) = expr.kind;
|
2021-12-17 06:40:22 -06:00
|
|
|
if count.ident.name == sym::count;
|
2021-07-01 11:17:38 -05:00
|
|
|
if let ExprKind::MethodCall(filter, _, [filter_recv, filter_arg], _) = count_recv.kind;
|
2019-05-17 16:53:54 -05:00
|
|
|
if filter.ident.name == sym!(filter);
|
2021-07-01 11:17:38 -05:00
|
|
|
if let ExprKind::Closure(_, _, body_id, _, _) = filter_arg.kind;
|
2021-04-08 10:50:13 -05:00
|
|
|
let body = cx.tcx.hir().body(body_id);
|
2021-07-01 11:17:38 -05:00
|
|
|
if let [param] = body.params;
|
|
|
|
if let PatKind::Binding(_, arg_id, _, _) = strip_pat_refs(param.pat).kind;
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Binary(ref op, l, r) = body.value.kind;
|
|
|
|
if op.node == BinOpKind::Eq;
|
|
|
|
if match_type(cx,
|
2021-07-01 11:17:38 -05:00
|
|
|
cx.typeck_results().expr_ty(filter_recv).peel_refs(),
|
2021-04-08 10:50:13 -05:00
|
|
|
&paths::SLICE_ITER);
|
2021-07-01 11:17:38 -05:00
|
|
|
let operand_is_arg = |expr| {
|
2021-12-17 06:40:22 -06:00
|
|
|
let expr = peel_ref_operators(cx, peel_blocks(expr));
|
2021-07-01 11:17:38 -05:00
|
|
|
path_to_local_id(expr, arg_id)
|
|
|
|
};
|
|
|
|
let needle = if operand_is_arg(l) {
|
|
|
|
r
|
|
|
|
} else if operand_is_arg(r) {
|
|
|
|
l
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
if ty::Uint(UintTy::U8) == *cx.typeck_results().expr_ty(needle).peel_refs().kind();
|
2021-09-08 09:31:47 -05:00
|
|
|
if !is_local_used(cx, needle, arg_id);
|
2017-10-23 14:18:02 -05:00
|
|
|
then {
|
2021-04-08 10:50:13 -05:00
|
|
|
let haystack = if let ExprKind::MethodCall(path, _, args, _) =
|
2021-07-01 11:17:38 -05:00
|
|
|
filter_recv.kind {
|
2021-04-08 10:50:13 -05:00
|
|
|
let p = path.ident.name;
|
|
|
|
if (p == sym::iter || p == sym!(iter_mut)) && args.len() == 1 {
|
|
|
|
&args[0]
|
|
|
|
} else {
|
2021-12-06 05:33:31 -06:00
|
|
|
filter_recv
|
2021-04-08 10:50:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-06 05:33:31 -06:00
|
|
|
filter_recv
|
2021-04-08 10:50:13 -05:00
|
|
|
};
|
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
NAIVE_BYTECOUNT,
|
|
|
|
expr.span,
|
|
|
|
"you appear to be counting bytes the naive way",
|
|
|
|
"consider using the bytecount crate",
|
|
|
|
format!("bytecount::count({}, {})",
|
|
|
|
snippet_with_applicability(cx, haystack.span, "..", &mut applicability),
|
|
|
|
snippet_with_applicability(cx, needle.span, "..", &mut applicability)),
|
|
|
|
applicability,
|
|
|
|
);
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
};
|
2017-08-22 16:45:08 -05:00
|
|
|
}
|
|
|
|
}
|