1 use crate::utils::{implements_trait, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_lint_pass, declare_tool_lint};
5 use rustc_errors::Applicability;
8 /// **What it does:** Checks for equal operands to comparison, logical and
9 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
10 /// `||`, `&`, `|`, `^`, `-` and `/`).
12 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
14 /// **Known problems:** False negatives: We had some false positives regarding
15 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
16 /// of `x.pop() && x.pop()`), so we removed matching any function or method
17 /// calls. We may introduce a whitelist of known pure functions in the future.
22 /// if x + 1 == x + 1 {}
26 "equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
29 declare_clippy_lint! {
30 /// **What it does:** Checks for arguments to `==` which have their address
31 /// taken to satisfy a bound
32 /// and suggests to dereference the other argument instead
34 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
36 /// **Known problems:** None
44 "taking a reference to satisfy the type constraints on `==`"
47 declare_lint_pass!(EqOp => [EQ_OP, OP_REF]);
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
50 #[allow(clippy::similar_names, clippy::too_many_lines)]
51 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
52 if let ExprKind::Binary(op, ref left, ref right) = e.kind {
53 if e.span.from_expansion() {
56 if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
61 &format!("equal expressions as operands to `{}`", op.node.as_str()),
65 let (trait_id, requires_ref) = match op.node {
66 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
67 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
68 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
69 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
70 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
71 // don't lint short circuiting ops
72 BinOpKind::And | BinOpKind::Or => return,
73 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
74 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
75 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
76 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
77 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
78 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
79 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => {
80 (cx.tcx.lang_items().ord_trait(), true)
83 if let Some(trait_id) = trait_id {
84 #[allow(clippy::match_same_arms)]
85 match (&left.kind, &right.kind) {
86 // do not suggest to dereference literals
87 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
89 (&ExprKind::AddrOf(_, _, ref l), &ExprKind::AddrOf(_, _, ref r)) => {
90 let lty = cx.tables.expr_ty(l);
91 let rty = cx.tables.expr_ty(r);
92 let lcpy = is_copy(cx, lty);
93 let rcpy = is_copy(cx, rty);
94 // either operator autorefs or both args are copyable
95 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
100 "needlessly taken reference of both operands",
102 let lsnip = snippet(cx, l.span, "...").to_string();
103 let rsnip = snippet(cx, r.span, "...").to_string();
106 "use the values directly".to_string(),
107 vec![(left.span, lsnip), (right.span, rsnip)],
113 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
115 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
116 let lsnip = snippet(cx, l.span, "...").to_string();
119 "use the left value directly",
121 Applicability::MaybeIncorrect, // FIXME #2597
126 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
132 "needlessly taken reference of right operand",
134 let rsnip = snippet(cx, r.span, "...").to_string();
137 "use the right value directly",
139 Applicability::MaybeIncorrect, // FIXME #2597
146 (&ExprKind::AddrOf(_, _, ref l), _) => {
147 let lty = cx.tables.expr_ty(l);
148 let lcpy = is_copy(cx, lty);
149 if (requires_ref || lcpy)
150 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
152 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
153 let lsnip = snippet(cx, l.span, "...").to_string();
156 "use the left value directly",
158 Applicability::MachineApplicable, // snippet
164 (_, &ExprKind::AddrOf(_, _, ref r)) => {
165 let rty = cx.tables.expr_ty(r);
166 let rcpy = is_copy(cx, rty);
167 if (requires_ref || rcpy)
168 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
170 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
171 let rsnip = snippet(cx, r.span, "...").to_string();
174 "use the right value directly",
176 Applicability::MachineApplicable, // snippet
188 fn is_valid_operator(op: BinOp) -> bool {
202 | BinOpKind::BitOr => true,