2 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use utils::{get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, SpanlessEq};
6 use 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",
98 |db| if let (Some(snip_a), Some(snip_r)) =
99 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
101 let a = &sugg::Sugg::hir(cx, assignee, "..");
102 let r = &sugg::Sugg::hir(cx, rhs, "..");
103 let long = format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
106 &format!("Did you mean {} = {} {} {} or {}? Consider replacing it with",
107 snip_a, snip_a, op.node.as_str(), snip_r,
109 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r)
120 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
123 // lhs op= l commutative_op r
124 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
130 hir::ExprAssign(ref assignee, ref e) => {
131 if let hir::ExprBinary(op, ref l, ref r) = e.node {
132 #[allow(cyclomatic_complexity)]
133 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
134 let ty = cx.tables.expr_ty(assignee);
135 let rty = cx.tables.expr_ty(rhs);
141 $($trait_name:ident:$full_trait_name:ident),+) => {
143 $(hir::$full_trait_name => {
144 let [krate, module] = ::utils::paths::OPS_MODULE;
145 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
146 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
149 return; // useless if the trait doesn't exist
151 // check that we are not inside an `impl AssignOp` of this exact operation
152 let parent_fn = cx.tcx.hir.get_parent(e.id);
153 let parent_impl = cx.tcx.hir.get_parent(parent_fn);
154 // the crate node is the only one that is not in the map
156 if parent_impl != ast::CRATE_NODE_ID;
157 if let hir::map::Node::NodeItem(item) = cx.tcx.hir.get(parent_impl);
158 if let hir::Item_::ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) =
160 if trait_ref.path.def.def_id() == trait_id;
163 implements_trait($cx, $ty, trait_id, &[$rty])
191 "manual implementation of an assign operation",
192 |db| if let (Some(snip_a), Some(snip_r)) =
193 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
198 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
205 let mut visitor = ExprVisitor {
211 walk_expr(&mut visitor, e);
213 if visitor.counter == 1 {
215 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
218 // a = b commutative_op a
219 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
241 fn is_commutative(op: hir::BinOp_) -> bool {
242 use rustc::hir::BinOp_::*;
244 BiAdd | BiMul | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr | BiEq | BiNe => true,
245 BiSub | BiDiv | BiRem | BiShl | BiShr | BiLt | BiLe | BiGe | BiGt => false,
249 struct ExprVisitor<'a, 'tcx: 'a> {
250 assignee: &'a hir::Expr,
252 cx: &'a LateContext<'a, 'tcx>,
255 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
256 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
257 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
261 walk_expr(self, expr);
263 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
264 NestedVisitorMap::None