1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::rustc_errors::Applicability;
15 implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq,
18 /// **What it does:** Checks for equal operands to comparison, logical and
19 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
20 /// `||`, `&`, `|`, `^`, `-` and `/`).
22 /// **Why is this bad?** This is usually just a typo or a copy and paste error.
24 /// **Known problems:** False negatives: We had some false positives regarding
25 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
26 /// of `x.pop() && x.pop()`), so we removed matching any function or method
27 /// calls. We may introduce a whitelist of known pure functions in the future.
33 declare_clippy_lint! {
36 "equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
39 /// **What it does:** Checks for arguments to `==` which have their address
40 /// taken to satisfy a bound
41 /// and suggests to dereference the other argument instead
43 /// **Why is this bad?** It is more idiomatic to dereference the other argument.
45 /// **Known problems:** None
51 declare_clippy_lint! {
54 "taking a reference to satisfy the type constraints on `==`"
57 #[derive(Copy, Clone)]
60 impl LintPass for EqOp {
61 fn get_lints(&self) -> LintArray {
62 lint_array!(EQ_OP, OP_REF)
66 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
67 #[allow(clippy::similar_names)]
68 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
69 if let ExprKind::Binary(op, ref left, ref right) = e.node {
73 if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
78 &format!("equal expressions as operands to `{}`", op.node.as_str()),
82 let (trait_id, requires_ref) = match op.node {
83 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
84 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
85 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
86 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
87 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
88 // don't lint short circuiting ops
89 BinOpKind::And | BinOpKind::Or => return,
90 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
91 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
92 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
93 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
94 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
95 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
96 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => {
97 (cx.tcx.lang_items().ord_trait(), true)
100 if let Some(trait_id) = trait_id {
101 #[allow(clippy::match_same_arms)]
102 match (&left.node, &right.node) {
103 // do not suggest to dereference literals
104 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
106 (&ExprKind::AddrOf(_, ref l), &ExprKind::AddrOf(_, ref r)) => {
107 let lty = cx.tables.expr_ty(l);
108 let rty = cx.tables.expr_ty(r);
109 let lcpy = is_copy(cx, lty);
110 let rcpy = is_copy(cx, rty);
111 // either operator autorefs or both args are copyable
112 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
117 "needlessly taken reference of both operands",
119 let lsnip = snippet(cx, l.span, "...").to_string();
120 let rsnip = snippet(cx, r.span, "...").to_string();
123 "use the values directly".to_string(),
124 vec![(left.span, lsnip), (right.span, rsnip)],
130 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
132 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
133 let lsnip = snippet(cx, l.span, "...").to_string();
134 db.span_suggestion_with_applicability(
136 "use the left value directly",
138 Applicability::MachineApplicable, // snippet
143 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
149 "needlessly taken reference of right operand",
151 let rsnip = snippet(cx, r.span, "...").to_string();
152 db.span_suggestion_with_applicability(
154 "use the right value directly",
156 Applicability::MachineApplicable, // snippet
163 (&ExprKind::AddrOf(_, ref l), _) => {
164 let lty = cx.tables.expr_ty(l);
165 let lcpy = is_copy(cx, lty);
166 if (requires_ref || lcpy)
167 && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right).into()])
169 span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
170 let lsnip = snippet(cx, l.span, "...").to_string();
171 db.span_suggestion_with_applicability(
173 "use the left value directly",
175 Applicability::MachineApplicable, // snippet
181 (_, &ExprKind::AddrOf(_, ref r)) => {
182 let rty = cx.tables.expr_ty(r);
183 let rcpy = is_copy(cx, rty);
184 if (requires_ref || rcpy)
185 && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty.into()])
187 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
188 let rsnip = snippet(cx, r.span, "...").to_string();
189 db.span_suggestion_with_applicability(
191 "use the right value directly",
193 Applicability::MachineApplicable, // snippet
205 fn is_valid_operator(op: BinOp) -> bool {
219 | BinOpKind::BitOr => true,