]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/eq_op.rs
update to the rust-PR that unblocks clippy
[rust.git] / clippy_lints / src / eq_op.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use utils::{SpanlessEq, span_lint};
4
5 /// **What it does:** Checks for equal operands to comparison, logical and
6 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
7 /// `||`, `&`, `|`, `^`, `-` and `/`).
8 ///
9 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
10 ///
11 /// **Known problems:** False negatives: We had some false positives regarding
12 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
13 /// of `x.pop() && x.pop()`), so we removed matching any function or method
14 /// calls. We may introduce a whitelist of known pure functions in the future.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// x + 1 == x + 1
19 /// ```
20 declare_lint! {
21     pub EQ_OP,
22     Warn,
23     "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
24 }
25
26 #[derive(Copy,Clone)]
27 pub struct EqOp;
28
29 impl LintPass for EqOp {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(EQ_OP)
32     }
33 }
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
36     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
37         if let ExprBinary(ref op, ref left, ref right) = e.node {
38             if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
39                 span_lint(cx,
40                           EQ_OP,
41                           e.span,
42                           &format!("equal expressions as operands to `{}`", op.node.as_str()));
43             }
44         }
45     }
46 }
47
48
49 fn is_valid_operator(op: &BinOp) -> bool {
50     match op.node {
51         BiSub |
52         BiDiv |
53         BiEq |
54         BiLt |
55         BiLe |
56         BiGt |
57         BiGe |
58         BiNe |
59         BiAnd |
60         BiOr |
61         BiBitXor |
62         BiBitAnd |
63         BiBitOr => true,
64         _ => false,
65     }
66 }