]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/eq_op.rs
Dogfood
[rust.git] / clippy_lints / src / eq_op.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use utils::{SpanlessEq, span_lint, span_lint_and_then, multispan_sugg, snippet, implements_trait};
4 use utils::sugg::Sugg;
5
6 /// **What it does:** Checks for equal operands to comparison, logical and
7 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
8 /// `||`, `&`, `|`, `^`, `-` and `/`).
9 ///
10 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
11 ///
12 /// **Known problems:** False negatives: We had some false positives regarding
13 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
14 /// of `x.pop() && x.pop()`), so we removed matching any function or method
15 /// calls. We may introduce a whitelist of known pure functions in the future.
16 ///
17 /// **Example:**
18 /// ```rust
19 /// x + 1 == x + 1
20 /// ```
21 declare_lint! {
22     pub EQ_OP,
23     Warn,
24     "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
25 }
26
27 /// **What it does:** Checks for arguments to `==` which have their address taken to satisfy a bound
28 /// and suggests to dereference the other argument instead
29 ///
30 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
31 ///
32 /// **Known problems:** None
33 ///
34 /// **Example:**
35 /// ```rust
36 /// &x == y
37 /// ```
38 declare_lint! {
39     pub OP_REF,
40     Warn,
41     "taking a reference to satisfy the type constraints on `==`"
42 }
43
44 #[derive(Copy,Clone)]
45 pub struct EqOp;
46
47 impl LintPass for EqOp {
48     fn get_lints(&self) -> LintArray {
49         lint_array!(EQ_OP, OP_REF)
50     }
51 }
52
53 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
54     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
55         if let ExprBinary(ref op, ref left, ref right) = e.node {
56             if is_valid_operator(op) {
57                 if SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
58                     span_lint(cx,
59                             EQ_OP,
60                             e.span,
61                             &format!("equal expressions as operands to `{}`", op.node.as_str()));
62                 } else {
63                     let trait_id = match op.node {
64                         BiAdd => cx.tcx.lang_items.add_trait(),
65                         BiSub => cx.tcx.lang_items.sub_trait(),
66                         BiMul => cx.tcx.lang_items.mul_trait(),
67                         BiDiv => cx.tcx.lang_items.div_trait(),
68                         BiRem => cx.tcx.lang_items.rem_trait(),
69                         BiAnd |
70                         BiOr => None,
71                         BiBitXor => cx.tcx.lang_items.bitxor_trait(),
72                         BiBitAnd => cx.tcx.lang_items.bitand_trait(),
73                         BiBitOr => cx.tcx.lang_items.bitor_trait(),
74                         BiShl => cx.tcx.lang_items.shl_trait(),
75                         BiShr => cx.tcx.lang_items.shr_trait(),
76                         BiNe |
77                         BiEq => cx.tcx.lang_items.eq_trait(),
78                         BiLt |
79                         BiLe |
80                         BiGe |
81                         BiGt => cx.tcx.lang_items.ord_trait(),
82                     };
83                     if let Some(trait_id) = trait_id {
84                         #[allow(match_same_arms)]
85                         match (&left.node, &right.node) {
86                             // do not suggest to dereference literals
87                             (&ExprLit(..), _) |
88                             (_, &ExprLit(..)) => {},
89                             // &foo == &bar
90                             (&ExprAddrOf(_, ref l), &ExprAddrOf(_, ref r)) => {
91                                 if implements_trait(cx, cx.tables.expr_ty(l), trait_id, &[cx.tables.expr_ty(r)], None) {
92                                     span_lint_and_then(cx,
93                                         OP_REF,
94                                         e.span,
95                                         "taken reference of both operands, which is done automatically by the operator anyway",
96                                         |db| {
97                                             let lsnip = snippet(cx, l.span, "...").to_string();
98                                             let rsnip = snippet(cx, r.span, "...").to_string();
99                                             multispan_sugg(db,
100                                                         "use the values directly".to_string(),
101                                                         vec![(left.span, lsnip),
102                                                             (right.span, rsnip)]);
103                                         }
104                                     )
105                                 }
106                             }
107                             // &foo == bar
108                             (&ExprAddrOf(_, ref l), _) => {
109                                 if implements_trait(cx, cx.tables.expr_ty(l), trait_id, &[cx.tables.expr_ty(right)], None) {
110                                     span_lint_and_then(cx,
111                                         OP_REF,
112                                         e.span,
113                                         "taken reference of left operand",
114                                         |db| {
115                                             let lsnip = snippet(cx, l.span, "...").to_string();
116                                             let rsnip = Sugg::hir(cx, right, "...").deref().to_string();
117                                             multispan_sugg(db,
118                                                         "dereference the right operand instead".to_string(),
119                                                         vec![(left.span, lsnip),
120                                                             (right.span, rsnip)]);
121                                         }
122                                     )
123                                 }
124                             }
125                             // foo == &bar
126                             (_, &ExprAddrOf(_, ref r)) => {
127                                 if implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[cx.tables.expr_ty(r)], None) {
128                                     span_lint_and_then(cx,
129                                         OP_REF,
130                                         e.span,
131                                         "taken reference of right operand",
132                                         |db| {
133                                             let lsnip = Sugg::hir(cx, left, "...").deref().to_string();
134                                             let rsnip = snippet(cx, r.span, "...").to_string();
135                                             multispan_sugg(db,
136                                                         "dereference the left operand instead".to_string(),
137                                                         vec![(left.span, lsnip),
138                                                             (right.span, rsnip)]);
139                                         }
140                                     )
141                                 }
142                             }
143                             _ => {}
144                         }
145                     }
146                 }
147             }
148         }
149     }
150 }
151
152
153 fn is_valid_operator(op: &BinOp) -> bool {
154     match op.node {
155         BiSub | BiDiv | BiEq | BiLt | BiLe | BiGt | BiGe | BiNe | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr => true,
156         _ => false,
157     }
158 }