]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/bytecount.rs
new lint: naive_bytecount
[rust.git] / clippy_lints / src / bytecount.rs
1 use consts::{constant, Constant};
2 use rustc_const_math::ConstInt;
3 use rustc::hir::*;
4 use rustc::lint::*;
5 use utils::{match_type, paths, snippet, span_lint_and_sugg, walk_ptrs_ty};
6
7 /// **What it does:** Checks for naive byte counts
8 ///
9 /// **Why is this bad?** The [`bytecount`](https://crates.io/crates/bytecount)
10 /// crate has methods to count your bytes faster, especially for large slices.
11 ///
12 /// **Known problems:** If you have predominantly small slices, the
13 /// `bytecount::count(..)` method may actually be slower. However, if you can
14 /// ensure that less than 2³²-1 matches arise, the `naive_count_32(..)` can be
15 /// faster in those cases.
16 ///
17 /// **Example:**
18 ///
19 /// ```rust
20 /// &my_data.filter(|&x| x == 0u8).count() // use bytecount::count instead
21 /// ```
22 declare_lint! {
23     pub NAIVE_BYTECOUNT,
24     Warn,
25     "use of naive `<slice>.filter(|&x| x == y).count()` to count byte values"
26 }
27
28 #[derive(Copy, Clone)]
29 pub struct ByteCount;
30
31 impl LintPass for ByteCount {
32     fn get_lints(&self) -> LintArray {
33         lint_array!(NAIVE_BYTECOUNT)
34     }
35 }
36
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
38     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
39         if_let_chain!([
40             let ExprMethodCall(ref count, _, ref count_args) = expr.node,
41             count.name == "count",
42             count_args.len() == 1,
43             let ExprMethodCall(ref filter, _, ref filter_args) = count_args[0].node,
44             filter.name == "filter",
45             filter_args.len() == 2,
46             let ExprClosure(_, _, body_id, _) = filter_args[1].node,
47         ], {
48             let body = cx.tcx.hir.body(body_id);
49             if_let_chain!([
50                 let ExprBinary(ref op, ref l, ref r) = body.value.node,
51                 op.node == BiEq,
52                 match_type(cx,
53                            walk_ptrs_ty(cx.tables.expr_ty(&filter_args[0])),
54                            &paths::SLICE_ITER),
55                 let Some((Constant::Int(ConstInt::U8(needle)), _)) =
56                         constant(cx, l).or_else(|| constant(cx, r))
57             ], {
58                 let haystack = if let ExprMethodCall(ref path, _, ref args) =
59                         filter_args[0].node {
60                     let p = path.name;
61                     if (p == "iter" || p == "iter_mut") && args.len() == 1 {
62                         &args[0]
63                     } else {
64                         &filter_args[0]
65                     }
66                 } else {
67                     &filter_args[0]
68                 };
69                 span_lint_and_sugg(cx,
70                                    NAIVE_BYTECOUNT,
71                                    expr.span,
72                                    "You appear to be counting bytes the naive way",
73                                    "Consider using the bytecount crate",
74                                    format!("bytecount::count({}, {})",
75                                             snippet(cx, haystack.span, ".."),
76                                             needle));
77             });
78         });
79     }
80 }