]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/bytecount.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / bytecount.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use rustc::ty;
4 use syntax::ast::{Name, UintTy};
5 use utils::{contains_name, match_type, paths, single_segment_path, 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 body.arguments.len() == 1,
51 let Some(argname) = get_pat_name(&body.arguments[0].pat),
52 let ExprBinary(ref op, ref l, ref r) = body.value.node,
53 op.node == BiEq,
54 match_type(cx,
55 walk_ptrs_ty(cx.tables.expr_ty(&filter_args[0])),
56 &paths::SLICE_ITER),
57 ], {
58 let needle = match get_path_name(l) {
59 Some(name) if check_arg(name, argname, r) => r,
60 _ => match get_path_name(r) {
61 Some(name) if check_arg(name, argname, l) => l,
62 _ => { return; }
63 }
64 };
65 if ty::TyUint(UintTy::U8) != walk_ptrs_ty(cx.tables.expr_ty(needle)).sty {
66 return;
67 }
68 let haystack = if let ExprMethodCall(ref path, _, ref args) =
69 filter_args[0].node {
70 let p = path.name;
71 if (p == "iter" || p == "iter_mut") && args.len() == 1 {
72 &args[0]
73 } else {
74 &filter_args[0]
75 }
76 } else {
77 &filter_args[0]
78 };
79 span_lint_and_sugg(cx,
80 NAIVE_BYTECOUNT,
81 expr.span,
82 "You appear to be counting bytes the naive way",
83 "Consider using the bytecount crate",
84 format!("bytecount::count({}, {})",
85 snippet(cx, haystack.span, ".."),
86 snippet(cx, needle.span, "..")));
87 });
88 });
89 }
90 }
91
92 fn check_arg(name: Name, arg: Name, needle: &Expr) -> bool {
93 name == arg && !contains_name(name, needle)
94 }
95
96 fn get_pat_name(pat: &Pat) -> Option<Name> {
97 match pat.node {
98 PatKind::Binding(_, _, ref spname, _) => Some(spname.node),
99 PatKind::Path(ref qpath) => single_segment_path(qpath).map(|ps| ps.name),
100 PatKind::Box(ref p) |
101 PatKind::Ref(ref p, _) => get_pat_name(&*p),
102 _ => None,
103 }
104 }
105
106 fn get_path_name(expr: &Expr) -> Option<Name> {
107 match expr.node {
108 ExprBox(ref e) |
109 ExprAddrOf(_, ref e) |
110 ExprUnary(UnOp::UnDeref, ref e) => get_path_name(e),
111 ExprBlock(ref b) => {
112 if b.stmts.is_empty() {
113 b.expr.as_ref().and_then(|p| get_path_name(p))
114 } else {
115 None
116 }
117 },
118 ExprPath(ref qpath) => single_segment_path(qpath).map(|ps| ps.name),
119 _ => None,
120 }
121 }