1 use if_chain::if_chain;
3 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6 use rustc_errors::Applicability;
9 get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
11 use crate::utils::{higher, sugg};
13 declare_clippy_lint! {
14 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
17 /// **Why is this bad?** These can be written as the shorter `a op= b`.
19 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
20 /// implementations that differ from the regular `Op` impl.
29 pub ASSIGN_OP_PATTERN,
31 "assigning the result of an operation on a variable to that same variable"
34 declare_clippy_lint! {
35 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
37 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
40 /// **Known problems:** Clippy cannot know for sure if `a op= a op b` should have
41 /// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore, it suggests both.
42 /// If `a op= a op b` is really the correct behaviour it should be
43 /// written as `a = a op a op b` as it's less confusing.
52 pub MISREFACTORED_ASSIGN_OP,
54 "having a variable on both sides of an assign op"
57 declare_lint_pass!(AssignOps => [ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP]);
59 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
60 #[allow(clippy::too_many_lines)]
61 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
63 hir::ExprKind::AssignOp(op, lhs, rhs) => {
64 if let hir::ExprKind::Binary(binop, l, r) = &rhs.node {
65 if op.node != binop.node {
69 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
70 lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, r);
72 // lhs op= l commutative_op r
73 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
74 lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, l);
78 hir::ExprKind::Assign(assignee, e) => {
79 if let hir::ExprKind::Binary(op, l, r) = &e.node {
80 #[allow(clippy::cognitive_complexity)]
81 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
82 let ty = cx.tables.expr_ty(assignee);
83 let rty = cx.tables.expr_ty(rhs);
89 $($trait_name:ident),+) => {
91 $(hir::BinOpKind::$trait_name => {
92 let [krate, module] = crate::utils::paths::OPS_MODULE;
93 let path: [&str; 3] = [krate, module, concat!(stringify!($trait_name), "Assign")];
94 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
97 return; // useless if the trait doesn't exist
99 // check that we are not inside an `impl AssignOp` of this exact operation
100 let parent_fn = cx.tcx.hir().get_parent_item(e.hir_id);
102 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
103 if trait_ref.path.res.def_id() == trait_id;
106 implements_trait($cx, $ty, trait_id, &[$rty])
134 "manual implementation of an assign operation",
136 if let (Some(snip_a), Some(snip_r)) =
137 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
142 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
143 Applicability::MachineApplicable,
151 let mut visitor = ExprVisitor {
157 walk_expr(&mut visitor, e);
159 if visitor.counter == 1 {
161 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
164 // a = b commutative_op a
165 // Limited to primitive type as these ops are know to be commutative
166 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r)
167 && cx.tables.expr_ty(assignee).is_primitive_ty()
171 | hir::BinOpKind::Mul
172 | hir::BinOpKind::And
174 | hir::BinOpKind::BitXor
175 | hir::BinOpKind::BitAnd
176 | hir::BinOpKind::BitOr => {
190 fn lint_misrefactored_assign_op(
191 cx: &LateContext<'_, '_>,
195 assignee: &hir::Expr,
196 rhs_other: &hir::Expr,
200 MISREFACTORED_ASSIGN_OP,
202 "variable appears on both sides of an assignment operation",
204 if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span)) {
205 let a = &sugg::Sugg::hir(cx, assignee, "..");
206 let r = &sugg::Sugg::hir(cx, rhs, "..");
207 let long = format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
211 "Did you mean {} = {} {} {} or {}? Consider replacing it with",
218 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
219 Applicability::MaybeIncorrect,
225 Applicability::MaybeIncorrect, // snippet
232 fn is_commutative(op: hir::BinOpKind) -> bool {
233 use rustc::hir::BinOpKind::*;
235 Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
236 Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,
240 struct ExprVisitor<'a, 'tcx> {
241 assignee: &'a hir::Expr,
243 cx: &'a LateContext<'a, 'tcx>,
246 impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
247 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
248 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
252 walk_expr(self, expr);
254 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
255 NestedVisitorMap::None