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_restriction_lint! {
23 "any compound assignment operation"
26 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
29 /// **Why is this bad?** These can be written as the shorter `a op= b`.
31 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
32 /// implementations that differ from the regular `Op` impl.
41 pub ASSIGN_OP_PATTERN,
43 "assigning the result of an operation on a variable to that same variable"
46 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
48 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
51 /// **Known problems:** Someone might actually mean `a op= a op b`, but that
52 /// should rather be written as `a = (2 * a) op b` where applicable.
61 pub MISREFACTORED_ASSIGN_OP,
63 "having a variable on both sides of an assign op"
66 #[derive(Copy, Clone, Default)]
69 impl LintPass for AssignOps {
70 fn get_lints(&self) -> LintArray {
71 lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
75 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
76 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
78 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
79 span_lint_and_then(cx, ASSIGN_OPS, expr.span, "assign operation detected", |db| {
80 let lhs = &sugg::Sugg::hir(cx, lhs, "..");
81 let rhs = &sugg::Sugg::hir(cx, rhs, "..");
86 format!("{} = {}", lhs, sugg::make_binop(higher::binop(op.node), lhs, rhs)),
89 if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
90 if op.node == binop.node {
91 let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
94 MISREFACTORED_ASSIGN_OP,
96 "variable appears on both sides of an assignment operation",
97 |db| if let (Some(snip_a), Some(snip_r)) =
98 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
100 let a = &sugg::Sugg::hir(cx, assignee, "..");
101 let r = &sugg::Sugg::hir(cx, rhs, "..");
102 let long = format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
105 &format!("Did you mean {} = {} {} {} or {}? Consider replacing it with",
106 snip_a, snip_a, op.node.as_str(), snip_r,
108 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r)
119 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
122 // lhs op= l commutative_op r
123 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
129 hir::ExprAssign(ref assignee, ref e) => {
130 if let hir::ExprBinary(op, ref l, ref r) = e.node {
131 #[allow(cyclomatic_complexity)]
132 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
133 let ty = cx.tables.expr_ty(assignee);
134 let rty = cx.tables.expr_ty(rhs);
140 $($trait_name:ident:$full_trait_name:ident),+) => {
142 $(hir::$full_trait_name => {
143 let [krate, module] = ::utils::paths::OPS_MODULE;
144 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
145 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
148 return; // useless if the trait doesn't exist
150 // check that we are not inside an `impl AssignOp` of this exact operation
151 let parent_fn = cx.tcx.hir.get_parent(e.id);
152 let parent_impl = cx.tcx.hir.get_parent(parent_fn);
153 // the crate node is the only one that is not in the map
155 if parent_impl != ast::CRATE_NODE_ID;
156 if let hir::map::Node::NodeItem(item) = cx.tcx.hir.get(parent_impl);
157 if let hir::Item_::ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) =
159 if trait_ref.path.def.def_id() == trait_id;
162 implements_trait($cx, $ty, trait_id, &[$rty])
190 "manual implementation of an assign operation",
191 |db| if let (Some(snip_a), Some(snip_r)) =
192 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
197 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
204 let mut visitor = ExprVisitor {
210 walk_expr(&mut visitor, e);
212 if visitor.counter == 1 {
214 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
217 // a = b commutative_op a
218 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
240 fn is_commutative(op: hir::BinOp_) -> bool {
241 use rustc::hir::BinOp_::*;
243 BiAdd | BiMul | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr | BiEq | BiNe => true,
244 BiSub | BiDiv | BiRem | BiShl | BiShr | BiLt | BiLe | BiGe | BiGt => false,
248 struct ExprVisitor<'a, 'tcx: 'a> {
249 assignee: &'a hir::Expr,
251 cx: &'a LateContext<'a, 'tcx>,
254 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
255 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
256 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
260 walk_expr(self, expr);
262 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
263 NestedVisitorMap::None