2 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use crate::utils::{get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, SpanlessEq};
6 use crate::utils::{higher, sugg};
8 /// **What it does:** Checks for compound assignment operations (`+=` and
11 /// **Why is this bad?** Projects with many developers from languages without
12 /// those operations may find them unreadable and not worth their weight.
14 /// **Known problems:** Types implementing `OpAssign` don't necessarily
21 declare_clippy_lint! {
24 "any compound assignment operation"
27 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
30 /// **Why is this bad?** These can be written as the shorter `a op= b`.
32 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
33 /// implementations that differ from the regular `Op` impl.
41 declare_clippy_lint! {
42 pub ASSIGN_OP_PATTERN,
44 "assigning the result of an operation on a variable to that same variable"
47 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
49 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
52 /// **Known problems:** Someone might actually mean `a op= a op b`, but that
53 /// should rather be written as `a = (2 * a) op b` where applicable.
61 declare_clippy_lint! {
62 pub MISREFACTORED_ASSIGN_OP,
64 "having a variable on both sides of an assign op"
67 #[derive(Copy, Clone, Default)]
70 impl LintPass for AssignOps {
71 fn get_lints(&self) -> LintArray {
72 lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
76 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
77 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
79 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
80 span_lint_and_then(cx, ASSIGN_OPS, expr.span, "assign operation detected", |db| {
81 let lhs = &sugg::Sugg::hir(cx, lhs, "..");
82 let rhs = &sugg::Sugg::hir(cx, rhs, "..");
87 format!("{} = {}", lhs, sugg::make_binop(higher::binop(op.node), lhs, rhs)),
90 if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
91 if op.node == binop.node {
92 let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
95 MISREFACTORED_ASSIGN_OP,
97 "variable appears on both sides of an assignment operation",
99 if let (Some(snip_a), Some(snip_r)) =
100 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
102 let a = &sugg::Sugg::hir(cx, assignee, "..");
103 let r = &sugg::Sugg::hir(cx, rhs, "..");
105 format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
109 "Did you mean {} = {} {} {} or {}? Consider replacing it with",
116 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
118 db.span_suggestion(expr.span, "or", long);
124 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
127 // lhs op= l commutative_op r
128 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
134 hir::ExprAssign(ref assignee, ref e) => {
135 if let hir::ExprBinary(op, ref l, ref r) = e.node {
136 #[allow(cyclomatic_complexity)]
137 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
138 let ty = cx.tables.expr_ty(assignee);
139 let rty = cx.tables.expr_ty(rhs);
145 $($trait_name:ident:$full_trait_name:ident),+) => {
147 $(hir::$full_trait_name => {
148 let [krate, module] = crate::utils::paths::OPS_MODULE;
149 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
150 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
153 return; // useless if the trait doesn't exist
155 // check that we are not inside an `impl AssignOp` of this exact operation
156 let parent_fn = cx.tcx.hir.get_parent(e.id);
157 let parent_impl = cx.tcx.hir.get_parent(parent_fn);
158 // the crate node is the only one that is not in the map
160 if parent_impl != ast::CRATE_NODE_ID;
161 if let hir::map::Node::NodeItem(item) = cx.tcx.hir.get(parent_impl);
162 if let hir::Item_::ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) =
164 if trait_ref.path.def.def_id() == trait_id;
167 implements_trait($cx, $ty, trait_id, &[$rty])
195 "manual implementation of an assign operation",
197 if let (Some(snip_a), Some(snip_r)) =
198 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
203 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
211 let mut visitor = ExprVisitor {
217 walk_expr(&mut visitor, e);
219 if visitor.counter == 1 {
221 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
224 // a = b commutative_op a
225 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
247 fn is_commutative(op: hir::BinOp_) -> bool {
248 use rustc::hir::BinOp_::*;
250 BiAdd | BiMul | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr | BiEq | BiNe => true,
251 BiSub | BiDiv | BiRem | BiShl | BiShr | BiLt | BiLe | BiGe | BiGt => false,
255 struct ExprVisitor<'a, 'tcx: 'a> {
256 assignee: &'a hir::Expr,
258 cx: &'a LateContext<'a, 'tcx>,
261 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
262 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
263 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
267 walk_expr(self, expr);
269 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
270 NestedVisitorMap::None