1 use crate::utils::{implements_trait, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq};
2 use rustc::declare_lint_pass;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc_errors::Applicability;
6 use rustc_session::declare_tool_lint;
9 /// **What it does:** Checks for equal operands to comparison, logical and
10 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
11 /// `||`, `&`, `|`, `^`, `-` and `/`).
13 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
15 /// **Known problems:** False negatives: We had some false positives regarding
16 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
17 /// of `x.pop() && x.pop()`), so we removed matching any function or method
18 /// calls. We may introduce a whitelist of known pure functions in the future.
23 /// if x + 1 == x + 1 {}
27 "equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
30 declare_clippy_lint! {
31 /// **What it does:** Checks for arguments to `==` which have their address
32 /// taken to satisfy a bound
33 /// and suggests to dereference the other argument instead
35 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
37 /// **Known problems:** None
45 "taking a reference to satisfy the type constraints on `==`"
48 declare_lint_pass!(EqOp => [EQ_OP, OP_REF]);
50 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
51 #[allow(clippy::similar_names, clippy::too_many_lines)]
52 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
53 if let ExprKind::Binary(op, ref left, ref right) = e.kind {
54 if e.span.from_expansion() {
57 if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
62 &format!("equal expressions as operands to `{}`", op.node.as_str()),
66 let (trait_id, requires_ref) = match op.node {
67 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
68 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
69 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
70 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
71 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
72 // don't lint short circuiting ops
73 BinOpKind::And | BinOpKind::Or => return,
74 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
75 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
76 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
77 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
78 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
79 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
80 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => {
81 (cx.tcx.lang_items().partial_ord_trait(), true)
84 if let Some(trait_id) = trait_id {
85 #[allow(clippy::match_same_arms)]
86 match (&left.kind, &right.kind) {
87 // do not suggest to dereference literals
88 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
90 (&ExprKind::AddrOf(BorrowKind::Ref, _, ref l), &ExprKind::AddrOf(BorrowKind::Ref, _, ref r)) => {
91 let lty = cx.tables.expr_ty(l);
92 let rty = cx.tables.expr_ty(r);
93 let lcpy = is_copy(cx, lty);
94 let rcpy = is_copy(cx, rty);
95 // either operator autorefs or both args are copyable
96 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
101 "needlessly taken reference of both operands",
103 let lsnip = snippet(cx, l.span, "...").to_string();
104 let rsnip = snippet(cx, r.span, "...").to_string();
107 "use the values directly".to_string(),
108 vec![(left.span, lsnip), (right.span, rsnip)],
114 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
116 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
117 let lsnip = snippet(cx, l.span, "...").to_string();
120 "use the left value directly",
122 Applicability::MaybeIncorrect, // FIXME #2597
127 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
133 "needlessly taken reference of right operand",
135 let rsnip = snippet(cx, r.span, "...").to_string();
138 "use the right value directly",
140 Applicability::MaybeIncorrect, // FIXME #2597
147 (&ExprKind::AddrOf(BorrowKind::Ref, _, ref l), _) => {
148 let lty = cx.tables.expr_ty(l);
149 let lcpy = is_copy(cx, lty);
150 if (requires_ref || lcpy)
151 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
153 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
154 let lsnip = snippet(cx, l.span, "...").to_string();
157 "use the left value directly",
159 Applicability::MaybeIncorrect, // FIXME #2597
165 (_, &ExprKind::AddrOf(BorrowKind::Ref, _, ref r)) => {
166 let rty = cx.tables.expr_ty(r);
167 let rcpy = is_copy(cx, rty);
168 if (requires_ref || rcpy)
169 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
171 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
172 let rsnip = snippet(cx, r.span, "...").to_string();
175 "use the right value directly",
177 Applicability::MaybeIncorrect, // FIXME #2597
189 fn is_valid_operator(op: BinOp) -> bool {
203 | BinOpKind::BitOr => true,