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 similar).
9 /// **Why is this bad?** Projects with many developers from languages without
10 /// those operations may find them unreadable and not worth their weight.
12 /// **Known problems:** Types implementing `OpAssign` don't necessarily implement `Op`.
18 declare_restriction_lint! {
20 "any compound assignment operation"
23 /// **What it does:** Checks for `a = a op b` or `a = b commutative_op a` patterns.
25 /// **Why is this bad?** These can be written as the shorter `a op= b`.
27 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have
28 /// implementations that differ from the regular `Op` impl.
37 pub ASSIGN_OP_PATTERN,
39 "assigning the result of an operation on a variable to that same variable"
42 /// **What it does:** Checks for `a op= a op b` or `a op= b op a` patterns.
44 /// **Why is this bad?** Most likely these are bugs where one meant to write `a op= b`.
46 /// **Known problems:** Someone might actually mean `a op= a op b`, but that
47 /// should rather be written as `a = (2 * a) op b` where applicable.
56 pub MISREFACTORED_ASSIGN_OP,
58 "having a variable on both sides of an assign op"
61 #[derive(Copy, Clone, Default)]
64 impl LintPass for AssignOps {
65 fn get_lints(&self) -> LintArray {
66 lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN, MISREFACTORED_ASSIGN_OP)
70 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
71 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
73 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
74 span_lint_and_then(cx, ASSIGN_OPS, expr.span, "assign operation detected", |db| {
75 let lhs = &sugg::Sugg::hir(cx, lhs, "..");
76 let rhs = &sugg::Sugg::hir(cx, rhs, "..");
78 db.span_suggestion(expr.span,
80 format!("{} = {}", lhs, sugg::make_binop(higher::binop(op.node), lhs, rhs)));
82 if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
83 if op.node == binop.node {
84 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
85 let ty = cx.tcx.tables().expr_ty(assignee);
86 if ty.walk_shallow().next().is_some() {
87 return; // implements_trait does not work with generics
89 let rty = cx.tcx.tables().expr_ty(rhs);
90 if rty.walk_shallow().next().is_some() {
91 return; // implements_trait does not work with generics
93 span_lint_and_then(cx,
94 MISREFACTORED_ASSIGN_OP,
96 "variable appears on both sides of an assignment operation",
98 if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span),
99 snippet_opt(cx, rhs.span)) {
100 db.span_suggestion(expr.span,
102 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r));
107 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
110 // lhs op= l commutative_op r
111 if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
117 hir::ExprAssign(ref assignee, ref e) => {
118 if let hir::ExprBinary(op, ref l, ref r) = e.node {
119 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
120 let ty = cx.tcx.tables().expr_ty(assignee);
121 if ty.walk_shallow().next().is_some() {
122 return; // implements_trait does not work with generics
124 let rty = cx.tcx.tables().expr_ty(rhs);
125 if rty.walk_shallow().next().is_some() {
126 return; // implements_trait does not work with generics
129 ($op:expr, $cx:expr, $ty:expr, $rty:expr, $($trait_name:ident:$full_trait_name:ident),+) => {
131 $(hir::$full_trait_name => {
132 let [krate, module] = ::utils::paths::OPS_MODULE;
133 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
134 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
137 return; // useless if the trait doesn't exist
139 // check that we are not inside an `impl AssignOp` of this exact operation
140 let parent_fn = cx.tcx.map.get_parent(e.id);
141 let parent_impl = cx.tcx.map.get_parent(parent_fn);
142 // the crate node is the only one that is not in the map
143 if parent_impl != ast::CRATE_NODE_ID {
144 if let hir::map::Node::NodeItem(item) = cx.tcx.map.get(parent_impl) {
145 if let hir::Item_::ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
146 if trait_ref.path.def.def_id() == trait_id {
152 implements_trait($cx, $ty, trait_id, vec![$rty])
174 span_lint_and_then(cx,
177 "manual implementation of an assign operation",
179 if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span),
180 snippet_opt(cx, rhs.span)) {
181 db.span_suggestion(expr.span,
183 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r));
189 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
192 // a = b commutative_op a
193 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
195 hir::BiAdd | hir::BiMul | hir::BiAnd | hir::BiOr | hir::BiBitXor | hir::BiBitAnd |
209 fn is_commutative(op: hir::BinOp_) -> bool {
210 use rustc::hir::BinOp_::*;