1 use crate::utils::{get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, SpanlessEq};
2 use crate::utils::{higher, sugg};
3 use if_chain::if_chain;
5 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_tool_lint, lint_array};
8 use rustc_errors::Applicability;
11 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
14 /// **Why is this bad?** These can be written as the shorter `a op= b`.
16 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
17 /// implementations that differ from the regular `Op` impl.
25 declare_clippy_lint! {
26 pub ASSIGN_OP_PATTERN,
28 "assigning the result of an operation on a variable to that same variable"
31 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
33 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
36 /// **Known problems:** Clippy cannot know for sure if `a op= a op b` should have
37 /// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore it suggests both.
38 /// If `a op= a op b` is really the correct behaviour it should be
39 /// written as `a = a op a op b` as it's less confusing.
47 declare_clippy_lint! {
48 pub MISREFACTORED_ASSIGN_OP,
50 "having a variable on both sides of an assign op"
53 #[derive(Copy, Clone, Default)]
56 impl LintPass for AssignOps {
57 fn get_lints(&self) -> LintArray {
58 lint_array!(ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
62 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
63 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
65 hir::ExprKind::AssignOp(op, lhs, rhs) => {
66 if let hir::ExprKind::Binary(binop, l, r) = &rhs.node {
67 if op.node == binop.node {
68 let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
71 MISREFACTORED_ASSIGN_OP,
73 "variable appears on both sides of an assignment operation",
75 if let (Some(snip_a), Some(snip_r)) =
76 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
78 let a = &sugg::Sugg::hir(cx, assignee, "..");
79 let r = &sugg::Sugg::hir(cx, rhs, "..");
81 format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
82 db.span_suggestion_with_applicability(
85 "Did you mean {} = {} {} {} or {}? Consider replacing it with",
92 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
93 Applicability::MachineApplicable,
95 db.span_suggestion_with_applicability(
99 Applicability::MachineApplicable, // snippet
106 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
109 // lhs op= l commutative_op r
110 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
116 hir::ExprKind::Assign(assignee, e) => {
117 if let hir::ExprKind::Binary(op, l, r) = &e.node {
118 #[allow(clippy::cyclomatic_complexity)]
119 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
120 let ty = cx.tables.expr_ty(assignee);
121 let rty = cx.tables.expr_ty(rhs);
127 $($trait_name:ident),+) => {
129 $(hir::BinOpKind::$trait_name => {
130 let [krate, module] = crate::utils::paths::OPS_MODULE;
131 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
132 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
135 return; // useless if the trait doesn't exist
137 // check that we are not inside an `impl AssignOp` of this exact operation
138 let parent_fn = cx.tcx.hir().get_parent(e.id);
139 let parent_impl = cx.tcx.hir().get_parent(parent_fn);
140 // the crate node is the only one that is not in the map
142 if parent_impl != ast::CRATE_NODE_ID;
143 if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl);
144 if let hir::ItemKind::Impl(_, _, _, _, Some(trait_ref), _, _) =
146 if trait_ref.path.def.def_id() == trait_id;
149 implements_trait($cx, $ty, trait_id, &[$rty])
177 "manual implementation of an assign operation",
179 if let (Some(snip_a), Some(snip_r)) =
180 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
182 db.span_suggestion_with_applicability(
185 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
186 Applicability::MachineApplicable,
194 let mut visitor = ExprVisitor {
200 walk_expr(&mut visitor, e);
202 if visitor.counter == 1 {
204 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
207 // a = b commutative_op a
208 // Limited to primitive type as these ops are know to be commutative
209 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r)
210 && cx.tables.expr_ty(assignee).is_primitive_ty()
214 | hir::BinOpKind::Mul
215 | hir::BinOpKind::And
217 | hir::BinOpKind::BitXor
218 | hir::BinOpKind::BitAnd
219 | hir::BinOpKind::BitOr => {
233 fn is_commutative(op: hir::BinOpKind) -> bool {
234 use rustc::hir::BinOpKind::*;
236 Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
237 Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,
241 struct ExprVisitor<'a, 'tcx: 'a> {
242 assignee: &'a hir::Expr,
244 cx: &'a LateContext<'a, 'tcx>,
247 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
248 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
249 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
253 walk_expr(self, expr);
255 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
256 NestedVisitorMap::None