2 implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq,
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
7 use rustc_errors::Applicability;
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.
24 declare_clippy_lint! {
27 "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
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
42 declare_clippy_lint! {
45 "taking a reference to satisfy the type constraints on `==`"
48 #[derive(Copy, Clone)]
51 impl LintPass for EqOp {
52 fn get_lints(&self) -> LintArray {
53 lint_array!(EQ_OP, OP_REF)
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
58 #[allow(clippy::similar_names)]
59 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
60 if let ExprKind::Binary(op, ref left, ref right) = e.node {
64 if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
69 &format!("equal expressions as operands to `{}`", op.node.as_str()),
73 let (trait_id, requires_ref) = match op.node {
74 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
75 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
76 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
77 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
78 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
79 // don't lint short circuiting ops
80 BinOpKind::And | BinOpKind::Or => return,
81 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
82 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
83 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
84 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
85 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
86 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
87 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => {
88 (cx.tcx.lang_items().ord_trait(), true)
91 if let Some(trait_id) = trait_id {
92 #[allow(clippy::match_same_arms)]
93 match (&left.node, &right.node) {
94 // do not suggest to dereference literals
95 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
97 (&ExprKind::AddrOf(_, ref l), &ExprKind::AddrOf(_, ref r)) => {
98 let lty = cx.tables.expr_ty(l);
99 let rty = cx.tables.expr_ty(r);
100 let lcpy = is_copy(cx, lty);
101 let rcpy = is_copy(cx, rty);
102 // either operator autorefs or both args are copyable
103 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
108 "needlessly taken reference of both operands",
110 let lsnip = snippet(cx, l.span, "...").to_string();
111 let rsnip = snippet(cx, r.span, "...").to_string();
114 "use the values directly".to_string(),
115 vec![(left.span, lsnip), (right.span, rsnip)],
121 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
123 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
124 let lsnip = snippet(cx, l.span, "...").to_string();
125 db.span_suggestion_with_applicability(
127 "use the left value directly",
129 Applicability::MachineApplicable, // snippet
134 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
140 "needlessly taken reference of right operand",
142 let rsnip = snippet(cx, r.span, "...").to_string();
143 db.span_suggestion_with_applicability(
145 "use the right value directly",
147 Applicability::MachineApplicable, // snippet
154 (&ExprKind::AddrOf(_, ref l), _) => {
155 let lty = cx.tables.expr_ty(l);
156 let lcpy = is_copy(cx, lty);
157 if (requires_ref || lcpy)
158 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
160 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
161 let lsnip = snippet(cx, l.span, "...").to_string();
162 db.span_suggestion_with_applicability(
164 "use the left value directly",
166 Applicability::MachineApplicable, // snippet
172 (_, &ExprKind::AddrOf(_, ref r)) => {
173 let rty = cx.tables.expr_ty(r);
174 let rcpy = is_copy(cx, rty);
175 if (requires_ref || rcpy)
176 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
178 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
179 let rsnip = snippet(cx, r.span, "...").to_string();
180 db.span_suggestion_with_applicability(
182 "use the right value directly",
184 Applicability::MachineApplicable, // snippet
196 fn is_valid_operator(op: BinOp) -> bool {
210 | BinOpKind::BitOr => true,