1 use clippy_utils::diagnostics::{multispan_sugg, span_lint, span_lint_and_then};
2 use clippy_utils::source::snippet;
3 use clippy_utils::ty::{implements_trait, is_copy};
5 ast_utils::is_useless_with_eq_exprs, eq_expr_value, higher, in_macro, is_expn_of, is_in_test_function,
7 use if_chain::if_chain;
8 use rustc_errors::Applicability;
9 use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, StmtKind};
10 use rustc_lint::{LateContext, LateLintPass};
11 use rustc_session::{declare_lint_pass, declare_tool_lint};
13 declare_clippy_lint! {
15 /// Checks for equal operands to comparison, logical and
16 /// bitwise, difference and division binary operators (`==`, `>`, etc., `&&`,
17 /// `||`, `&`, `|`, `^`, `-` and `/`).
19 /// ### Why is this bad?
20 /// This is usually just a typo or a copy and paste error.
22 /// ### Known problems
23 /// False negatives: We had some false positives regarding
24 /// calls (notably [racer](https://github.com/phildawes/racer) had one instance
25 /// of `x.pop() && x.pop()`), so we removed matching any function or method
26 /// calls. We may introduce a list of known pure functions in the future.
31 /// if x + 1 == x + 1 {}
41 "equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
44 declare_clippy_lint! {
46 /// Checks for arguments to `==` which have their address
47 /// taken to satisfy a bound
48 /// and suggests to dereference the other argument instead
50 /// ### Why is this bad?
51 /// It is more idiomatic to dereference the other argument.
53 /// ### Known problems
66 "taking a reference to satisfy the type constraints on `==`"
69 declare_lint_pass!(EqOp => [EQ_OP, OP_REF]);
71 const ASSERT_MACRO_NAMES: [&str; 4] = ["assert_eq", "assert_ne", "debug_assert_eq", "debug_assert_ne"];
73 impl<'tcx> LateLintPass<'tcx> for EqOp {
74 #[allow(clippy::similar_names, clippy::too_many_lines)]
75 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
76 if let ExprKind::Block(block, _) = e.kind {
77 for stmt in block.stmts {
78 for amn in &ASSERT_MACRO_NAMES {
80 if is_expn_of(stmt.span, amn).is_some();
81 if let StmtKind::Semi(matchexpr) = stmt.kind;
82 if let Some(macro_args) = higher::extract_assert_macro_args(matchexpr);
83 if macro_args.len() == 2;
84 let (lhs, rhs) = (macro_args[0], macro_args[1]);
85 if eq_expr_value(cx, lhs, rhs);
86 if !is_in_test_function(cx.tcx, e.hir_id);
91 lhs.span.to(rhs.span),
92 &format!("identical args used in this `{}!` macro call", amn),
99 if let ExprKind::Binary(op, left, right) = e.kind {
100 if e.span.from_expansion() {
103 let macro_with_not_op = |expr_kind: &ExprKind<'_>| {
104 if let ExprKind::Unary(_, expr) = *expr_kind {
110 if macro_with_not_op(&left.kind) || macro_with_not_op(&right.kind) {
113 if is_useless_with_eq_exprs(op.node.into())
114 && eq_expr_value(cx, left, right)
115 && !is_in_test_function(cx.tcx, e.hir_id)
121 &format!("equal expressions as operands to `{}`", op.node.as_str()),
125 let (trait_id, requires_ref) = match op.node {
126 BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
127 BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
128 BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
129 BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
130 BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
131 // don't lint short circuiting ops
132 BinOpKind::And | BinOpKind::Or => return,
133 BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
134 BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
135 BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
136 BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
137 BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
138 BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
139 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => {
140 (cx.tcx.lang_items().partial_ord_trait(), true)
143 if let Some(trait_id) = trait_id {
144 #[allow(clippy::match_same_arms)]
145 match (&left.kind, &right.kind) {
146 // do not suggest to dereference literals
147 (&ExprKind::Lit(..), _) | (_, &ExprKind::Lit(..)) => {},
149 (&ExprKind::AddrOf(BorrowKind::Ref, _, l), &ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
150 let lty = cx.typeck_results().expr_ty(l);
151 let rty = cx.typeck_results().expr_ty(r);
152 let lcpy = is_copy(cx, lty);
153 let rcpy = is_copy(cx, rty);
154 // either operator autorefs or both args are copyable
155 if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty.into()]) {
160 "needlessly taken reference of both operands",
162 let lsnip = snippet(cx, l.span, "...").to_string();
163 let rsnip = snippet(cx, r.span, "...").to_string();
166 "use the values directly",
167 vec![(left.span, lsnip), (right.span, rsnip)],
173 && implements_trait(cx, lty, trait_id, &[cx.typeck_results().expr_ty(right).into()])
179 "needlessly taken reference of left operand",
181 let lsnip = snippet(cx, l.span, "...").to_string();
182 diag.span_suggestion(
184 "use the left value directly",
186 Applicability::MaybeIncorrect, // FIXME #2597
192 && implements_trait(cx, cx.typeck_results().expr_ty(left), trait_id, &[rty.into()])
198 "needlessly taken reference of right operand",
200 let rsnip = snippet(cx, r.span, "...").to_string();
201 diag.span_suggestion(
203 "use the right value directly",
205 Applicability::MaybeIncorrect, // FIXME #2597
212 (&ExprKind::AddrOf(BorrowKind::Ref, _, l), _) => {
213 let lty = cx.typeck_results().expr_ty(l);
214 let lcpy = is_copy(cx, lty);
215 if (requires_ref || lcpy)
216 && implements_trait(cx, lty, trait_id, &[cx.typeck_results().expr_ty(right).into()])
222 "needlessly taken reference of left operand",
224 let lsnip = snippet(cx, l.span, "...").to_string();
225 diag.span_suggestion(
227 "use the left value directly",
229 Applicability::MaybeIncorrect, // FIXME #2597
236 (_, &ExprKind::AddrOf(BorrowKind::Ref, _, r)) => {
237 let rty = cx.typeck_results().expr_ty(r);
238 let rcpy = is_copy(cx, rty);
239 if (requires_ref || rcpy)
240 && implements_trait(cx, cx.typeck_results().expr_ty(left), trait_id, &[rty.into()])
242 span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |diag| {
243 let rsnip = snippet(cx, r.span, "...").to_string();
244 diag.span_suggestion(
246 "use the right value directly",
248 Applicability::MaybeIncorrect, // FIXME #2597