1 use crate::rustc::hir::*;
2 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use crate::rustc::{declare_tool_lint, lint_array};
4 use crate::utils::{in_macro, implements_trait, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq};
5 use crate::rustc_errors::Applicability;
7 /// **What it does:** Checks for equal operands to comparison, logical and
8 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
9 /// `||`, `&`, `|`, `^`, `-` and `/`).
11 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
13 /// **Known problems:** False negatives: We had some false positives regarding
14 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
15 /// of `x.pop() && x.pop()`), so we removed matching any function or method
16 /// calls. We may introduce a whitelist of known pure functions in the future.
22 declare_clippy_lint! {
25 "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
28 /// **What it does:** Checks for arguments to `==` which have their address
29 /// taken to satisfy a bound
30 /// and suggests to dereference the other argument instead
32 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
34 /// **Known problems:** None
40 declare_clippy_lint! {
43 "taking a reference to satisfy the type constraints on `==`"
46 #[derive(Copy, Clone)]
49 impl LintPass for EqOp {
50 fn get_lints(&self) -> LintArray {
51 lint_array!(EQ_OP, OP_REF)
55 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
56 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
57 if let ExprKind::Binary(op, ref left, ref right) = e.node {
61 if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
66 &format!("equal expressions as operands to `{}`", op.node.as_str()),
70 let (trait_id, requires_ref) = match op.node {
71 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
72 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
73 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
74 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
75 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
76 // don't lint short circuiting ops
77 BinOpKind::And | BinOpKind::Or => return,
78 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
79 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
80 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
81 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
82 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
83 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
84 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => (cx.tcx.lang_items().ord_trait(), true),
86 if let Some(trait_id) = trait_id {
87 #[allow(clippy::match_same_arms)]
88 match (&left.node, &right.node) {
89 // do not suggest to dereference literals
90 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
92 (&ExprKind::AddrOf(_, ref l), &ExprKind::AddrOf(_, ref r)) => {
93 let lty = cx.tables.expr_ty(l);
94 let rty = cx.tables.expr_ty(r);
95 let lcpy = is_copy(cx, lty);
96 let rcpy = is_copy(cx, rty);
97 // either operator autorefs or both args are copyable
98 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
103 "needlessly taken reference of both operands",
105 let lsnip = snippet(cx, l.span, "...").to_string();
106 let rsnip = snippet(cx, r.span, "...").to_string();
109 "use the values directly".to_string(),
110 vec![(left.span, lsnip), (right.span, rsnip)],
114 } else if lcpy && !rcpy && 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();
117 db.span_suggestion_with_applicability(
119 "use the left value directly",
121 Applicability::MachineApplicable, // snippet
124 } else if !lcpy && rcpy && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()]) {
129 "needlessly taken reference of right operand",
131 let rsnip = snippet(cx, r.span, "...").to_string();
132 db.span_suggestion_with_applicability(
134 "use the right value directly",
136 Applicability::MachineApplicable, // snippet
143 (&ExprKind::AddrOf(_, ref l), _) => {
144 let lty = cx.tables.expr_ty(l);
145 let lcpy = is_copy(cx, lty);
146 if (requires_ref || lcpy) && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()]) {
147 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
148 let lsnip = snippet(cx, l.span, "...").to_string();
149 db.span_suggestion_with_applicability(
151 "use the left value directly",
153 Applicability::MachineApplicable, // snippet
159 (_, &ExprKind::AddrOf(_, ref r)) => {
160 let rty = cx.tables.expr_ty(r);
161 let rcpy = is_copy(cx, rty);
162 if (requires_ref || rcpy) && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()]) {
163 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
164 let rsnip = snippet(cx, r.span, "...").to_string();
165 db.span_suggestion_with_applicability(
167 "use the right value directly",
169 Applicability::MachineApplicable, // snippet
182 fn is_valid_operator(op: BinOp) -> bool {
184 BinOpKind::Sub | BinOpKind::Div | BinOpKind::Eq | BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge | BinOpKind::Ne | BinOpKind::And | BinOpKind::Or | BinOpKind::BitXor | BinOpKind::BitAnd | BinOpKind::BitOr => true,