4 use utils::{span_lint_and_then, snippet_opt, SpanlessEq, get_trait_def_id, implements_trait};
5 use utils::{higher, sugg};
7 /// **What it does:** Checks for compound assignment operations (`+=` and
10 /// **Why is this bad?** Projects with many developers from languages without
11 /// those operations may find them unreadable and not worth their weight.
13 /// **Known problems:** Types implementing `OpAssign` don't necessarily
20 declare_restriction_lint! {
22 "any compound assignment operation"
25 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
28 /// **Why is this bad?** These can be written as the shorter `a op= b`.
30 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
31 /// implementations that differ from the regular `Op` impl.
40 pub ASSIGN_OP_PATTERN,
42 "assigning the result of an operation on a variable to that same variable"
45 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
47 /// **Why is this bad?** Most likely these are bugs where one meant to write `a
50 /// **Known problems:** Someone might actually mean `a op= a op b`, but that
51 /// should rather be written as `a = (2 * a) op b` where applicable.
60 pub MISREFACTORED_ASSIGN_OP,
62 "having a variable on both sides of an assign op"
65 #[derive(Copy, Clone, Default)]
68 impl LintPass for AssignOps {
69 fn get_lints(&self) -> LintArray {
70 lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
74 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
75 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
77 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
78 span_lint_and_then(cx, ASSIGN_OPS, expr.span, "assign operation detected", |db| {
79 let lhs = &sugg::Sugg::hir(cx, lhs, "..");
80 let rhs = &sugg::Sugg::hir(cx, rhs, "..");
85 format!("{} = {}", lhs, sugg::make_binop(higher::binop(op.node), lhs, rhs)),
88 if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
89 if op.node == binop.node {
90 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
91 span_lint_and_then(cx,
92 MISREFACTORED_ASSIGN_OP,
94 "variable appears on both sides of an assignment operation",
95 |db| if let (Some(snip_a), Some(snip_r)) =
96 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span)) {
97 db.span_suggestion(expr.span,
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::ExprAssign(ref assignee, ref e) => {
117 if let hir::ExprBinary(op, ref l, ref r) = e.node {
118 #[allow(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:$full_trait_name:ident),+) => {
129 $(hir::$full_trait_name => {
130 let [krate, module] = ::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 parent_impl != ast::CRATE_NODE_ID,
143 let hir::map::Node::NodeItem(item) = cx.tcx.hir.get(parent_impl),
144 let hir::Item_::ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) = item.node,
145 trait_ref.path.def.def_id() == trait_id
147 implements_trait($cx, $ty, trait_id, &[$rty])
176 "manual implementation of an assign operation",
177 |db| if let (Some(snip_a), Some(snip_r)) =
178 (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span))
183 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
190 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
193 // a = b commutative_op a
194 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
196 hir::BiAdd | hir::BiMul | hir::BiAnd | hir::BiOr | hir::BiBitXor | hir::BiBitAnd |
210 fn is_commutative(op: hir::BinOp_) -> bool {
211 use rustc::hir::BinOp_::*;
213 BiAdd | BiMul | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr | BiEq | BiNe => true,
214 BiSub | BiDiv | BiRem | BiShl | BiShr | BiLt | BiLe | BiGe | BiGt => false,