3 use utils::{SpanlessEq, span_lint, span_lint_and_then, multispan_sugg, snippet, implements_trait};
6 /// **What it does:** Checks for equal operands to comparison, logical and
7 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
8 /// `||`, `&`, `|`, `^`, `-` and `/`).
10 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
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.
24 "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
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
30 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
32 /// **Known problems:** None
41 "taking a reference to satisfy the type constraints on `==`"
47 impl LintPass for EqOp {
48 fn get_lints(&self) -> LintArray {
49 lint_array!(EQ_OP, OP_REF)
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) {
61 &format!("equal expressions as operands to `{}`", op.node.as_str()));
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(),
70 BiBitXor => cx.tcx.lang_items.bitxor_trait(),
71 BiBitAnd => cx.tcx.lang_items.bitand_trait(),
72 BiBitOr => cx.tcx.lang_items.bitor_trait(),
73 BiShl => cx.tcx.lang_items.shl_trait(),
74 BiShr => cx.tcx.lang_items.shr_trait(),
75 BiNe | BiEq => cx.tcx.lang_items.eq_trait(),
76 BiLt | BiLe | BiGe | BiGt => cx.tcx.lang_items.ord_trait(),
78 if let Some(trait_id) = trait_id {
79 #[allow(match_same_arms)]
80 match (&left.node, &right.node) {
81 // do not suggest to dereference literals
83 (_, &ExprLit(..)) => {},
85 (&ExprAddrOf(_, ref l), &ExprAddrOf(_, ref r)) => {
86 if implements_trait(cx, cx.tables.expr_ty(l), trait_id, &[cx.tables.expr_ty(r)], None) {
87 span_lint_and_then(cx,
90 "taken reference of both operands, which is done automatically \
91 by the operator anyway",
93 let lsnip = snippet(cx, l.span, "...").to_string();
94 let rsnip = snippet(cx, r.span, "...").to_string();
96 "use the values directly".to_string(),
97 vec![(left.span, lsnip),
98 (right.span, rsnip)]);
103 (&ExprAddrOf(_, ref l), _) => {
104 if implements_trait(cx,
105 cx.tables.expr_ty(l),
107 &[cx.tables.expr_ty(right)],
109 span_lint_and_then(cx, OP_REF, e.span, "taken reference of left operand", |db| {
110 let lsnip = snippet(cx, l.span, "...").to_string();
111 let rsnip = Sugg::hir(cx, right, "...").deref().to_string();
113 "dereference the right operand instead".to_string(),
114 vec![(left.span, lsnip),
115 (right.span, rsnip)]);
120 (_, &ExprAddrOf(_, ref r)) => {
121 if implements_trait(cx,
122 cx.tables.expr_ty(left),
124 &[cx.tables.expr_ty(r)],
126 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
127 let lsnip = Sugg::hir(cx, left, "...").deref().to_string();
128 let rsnip = snippet(cx, r.span, "...").to_string();
130 "dereference the left operand instead".to_string(),
131 vec![(left.span, lsnip),
132 (right.span, rsnip)]);
146 fn is_valid_operator(op: &BinOp) -> bool {
148 BiSub | BiDiv | BiEq | BiLt | BiLe | BiGt | BiGe | BiNe | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr => true,