2 implements_trait, in_macro_or_desugar, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq,
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
7 use rustc_errors::Applicability;
10 /// **What it does:** Checks for equal operands to comparison, logical and
11 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
12 /// `||`, `&`, `|`, `^`, `-` and `/`).
14 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
16 /// **Known problems:** False negatives: We had some false positives regarding
17 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
18 /// of `x.pop() && x.pop()`), so we removed matching any function or method
19 /// calls. We may introduce a whitelist of known pure functions in the future.
24 /// if x + 1 == x + 1 {}
28 "equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
31 declare_clippy_lint! {
32 /// **What it does:** Checks for arguments to `==` which have their address
33 /// taken to satisfy a bound
34 /// and suggests to dereference the other argument instead
36 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
38 /// **Known problems:** None
46 "taking a reference to satisfy the type constraints on `==`"
49 declare_lint_pass!(EqOp => [EQ_OP, OP_REF]);
51 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
52 #[allow(clippy::similar_names, clippy::too_many_lines)]
53 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
54 if let ExprKind::Binary(op, ref left, ref right) = e.node {
55 if in_macro_or_desugar(e.span) {
58 if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
63 &format!("equal expressions as operands to `{}`", op.node.as_str()),
67 let (trait_id, requires_ref) = match op.node {
68 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
69 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
70 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
71 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
72 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
73 // don't lint short circuiting ops
74 BinOpKind::And | BinOpKind::Or => return,
75 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
76 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
77 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
78 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
79 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
80 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
81 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => {
82 (cx.tcx.lang_items().ord_trait(), true)
85 if let Some(trait_id) = trait_id {
86 #[allow(clippy::match_same_arms)]
87 match (&left.node, &right.node) {
88 // do not suggest to dereference literals
89 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
91 (&ExprKind::AddrOf(_, ref l), &ExprKind::AddrOf(_, ref r)) => {
92 let lty = cx.tables.expr_ty(l);
93 let rty = cx.tables.expr_ty(r);
94 let lcpy = is_copy(cx, lty);
95 let rcpy = is_copy(cx, rty);
96 // either operator autorefs or both args are copyable
97 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
102 "needlessly taken reference of both operands",
104 let lsnip = snippet(cx, l.span, "...").to_string();
105 let rsnip = snippet(cx, r.span, "...").to_string();
108 "use the values directly".to_string(),
109 vec![(left.span, lsnip), (right.span, rsnip)],
115 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
117 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
118 let lsnip = snippet(cx, l.span, "...").to_string();
121 "use the left value directly",
123 Applicability::MachineApplicable, // snippet
128 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
134 "needlessly taken reference of right operand",
136 let rsnip = snippet(cx, r.span, "...").to_string();
139 "use the right value directly",
141 Applicability::MachineApplicable, // snippet
148 (&ExprKind::AddrOf(_, ref l), _) => {
149 let lty = cx.tables.expr_ty(l);
150 let lcpy = is_copy(cx, lty);
151 if (requires_ref || lcpy)
152 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
154 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
155 let lsnip = snippet(cx, l.span, "...").to_string();
158 "use the left value directly",
160 Applicability::MachineApplicable, // snippet
166 (_, &ExprKind::AddrOf(_, ref r)) => {
167 let rty = cx.tables.expr_ty(r);
168 let rcpy = is_copy(cx, rty);
169 if (requires_ref || rcpy)
170 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
172 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
173 let rsnip = snippet(cx, r.span, "...").to_string();
176 "use the right value directly",
178 Applicability::MachineApplicable, // snippet
190 fn is_valid_operator(op: BinOp) -> bool {
204 | BinOpKind::BitOr => true,