3 use utils::{SpanlessEq, span_lint, span_lint_and_then, multispan_sugg, snippet, implements_trait, is_copy};
5 /// **What it does:** Checks for equal operands to comparison, logical and
6 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
7 /// `||`, `&`, `|`, `^`, `-` and `/`).
9 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
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.
23 "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
26 /// **What it does:** Checks for arguments to `==` which have their address taken to satisfy a bound
27 /// and suggests to dereference the other argument instead
29 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
31 /// **Known problems:** None
40 "taking a reference to satisfy the type constraints on `==`"
46 impl LintPass for EqOp {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(EQ_OP, OP_REF)
52 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
53 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
54 if let ExprBinary(ref op, ref left, ref right) = e.node {
55 if is_valid_operator(op) {
56 if SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
60 &format!("equal expressions as operands to `{}`", op.node.as_str()));
64 let (trait_id, requires_ref) = match op.node {
65 BiAdd => (cx.tcx.lang_items.add_trait(), false),
66 BiSub => (cx.tcx.lang_items.sub_trait(), false),
67 BiMul => (cx.tcx.lang_items.mul_trait(), false),
68 BiDiv => (cx.tcx.lang_items.div_trait(), false),
69 BiRem => (cx.tcx.lang_items.rem_trait(), false),
70 // don't lint short circuiting ops
71 BiAnd | BiOr => return,
72 BiBitXor => (cx.tcx.lang_items.bitxor_trait(), false),
73 BiBitAnd => (cx.tcx.lang_items.bitand_trait(), false),
74 BiBitOr => (cx.tcx.lang_items.bitor_trait(), false),
75 BiShl => (cx.tcx.lang_items.shl_trait(), false),
76 BiShr => (cx.tcx.lang_items.shr_trait(), false),
77 BiNe | BiEq => (cx.tcx.lang_items.eq_trait(), true),
78 BiLt | BiLe | BiGe | BiGt => (cx.tcx.lang_items.ord_trait(), true),
80 let parent = cx.tcx.hir.get_parent(e.id);
81 if let Some(trait_id) = trait_id {
82 #[allow(match_same_arms)]
83 match (&left.node, &right.node) {
84 // do not suggest to dereference literals
86 (_, &ExprLit(..)) => {},
88 (&ExprAddrOf(_, ref l), &ExprAddrOf(_, ref r)) => {
89 let lty = cx.tables.expr_ty(l);
90 let rty = cx.tables.expr_ty(r);
91 let lcpy = is_copy(cx, lty, parent);
92 let rcpy = is_copy(cx, rty, parent);
93 // either operator autorefs or both args are copyable
94 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty], None) {
95 span_lint_and_then(cx,
98 "needlessly taken reference of both operands",
100 let lsnip = snippet(cx, l.span, "...").to_string();
101 let rsnip = snippet(cx, r.span, "...").to_string();
103 "use the values directly".to_string(),
104 vec![(left.span, lsnip),
105 (right.span, rsnip)]);
107 } else if lcpy && !rcpy && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) {
108 span_lint_and_then(cx,
111 "needlessly taken reference of left operand",
113 let lsnip = snippet(cx, l.span, "...").to_string();
114 db.span_suggestion(left.span, "use the left value directly", lsnip);
116 } else if !lcpy && rcpy && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) {
117 span_lint_and_then(cx,
120 "needlessly taken reference of right operand",
122 let rsnip = snippet(cx, r.span, "...").to_string();
123 db.span_suggestion(right.span, "use the right value directly", rsnip);
128 (&ExprAddrOf(_, ref l), _) => {
129 let lty = cx.tables.expr_ty(l);
130 let lcpy = is_copy(cx, lty, parent);
131 if (requires_ref || lcpy) && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) {
132 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
133 let lsnip = snippet(cx, l.span, "...").to_string();
134 db.span_suggestion(left.span, "use the left value directly", lsnip);
139 (_, &ExprAddrOf(_, ref r)) => {
140 let rty = cx.tables.expr_ty(r);
141 let rcpy = is_copy(cx, rty, parent);
142 if (requires_ref || rcpy) && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) {
143 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
144 let rsnip = snippet(cx, r.span, "...").to_string();
145 db.span_suggestion(left.span, "use the right value directly", rsnip);
157 fn is_valid_operator(op: &BinOp) -> bool {
159 BiSub | BiDiv | BiEq | BiLt | BiLe | BiGt | BiGe | BiNe | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr => true,