3 use utils::{span_lint_and_then, span_lint, snippet_opt, SpanlessEq, get_trait_def_id, implements_trait};
5 /// **What it does:** This lint checks for `+=` operations and similar
7 /// **Why is this bad?** Projects with many developers from languages without those operations
8 /// may find them unreadable and not worth their weight
10 /// **Known problems:** Types implementing `OpAssign` don't necessarily implement `Op`
16 declare_restriction_lint! {
18 "Any assignment operation"
21 /// **What it does:** Check for `a = a op b` or `a = b commutative_op a` patterns
23 /// **Why is this bad?** These can be written as the shorter `a op= b`
25 /// **Known problems:** While forbidden by the spec, `OpAssign` traits may have implementations that differ from the regular `Op` impl
35 pub ASSIGN_OP_PATTERN,
37 "assigning the result of an operation on a variable to that same variable"
40 #[derive(Copy, Clone, Default)]
43 impl LintPass for AssignOps {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN)
49 impl LateLintPass for AssignOps {
50 fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
52 hir::ExprAssignOp(op, ref lhs, ref rhs) => {
53 if let (Some(l), Some(r)) = (snippet_opt(cx, lhs.span), snippet_opt(cx, rhs.span)) {
54 span_lint_and_then(cx,
57 "assign operation detected",
60 hir::ExprBinary(op2, _, _) if op2 != op => {
61 db.span_suggestion(expr.span,
63 format!("{} = {} {} ({})", l, l, op.node.as_str(), r));
66 db.span_suggestion(expr.span,
68 format!("{} = {} {} {}", l, l, op.node.as_str(), r));
76 "assign operation detected");
79 hir::ExprAssign(ref assignee, ref e) => {
80 if let hir::ExprBinary(op, ref l, ref r) = e.node {
81 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
82 let ty = cx.tcx.expr_ty(assignee);
83 if ty.walk_shallow().next().is_some() {
84 return; // implements_trait does not work with generics
86 let rty = cx.tcx.expr_ty(rhs);
87 if rty.walk_shallow().next().is_some() {
88 return; // implements_trait does not work with generics
91 ($op:expr, $cx:expr, $ty:expr, $rty:expr, $($trait_name:ident:$full_trait_name:ident),+) => {
93 $(hir::$full_trait_name => {
94 let [krate, module] = ::utils::paths::OPS_MODULE;
95 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
96 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
99 return; // useless if the trait doesn't exist
101 implements_trait($cx, $ty, trait_id, vec![$rty])
107 if ops!(op.node, cx, ty, rty, Add:BiAdd,
120 if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs.span)) {
121 span_lint_and_then(cx,
124 "manual implementation of an assign operation",
126 db.span_suggestion(expr.span,
128 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r));
134 "manual implementation of an assign operation");
139 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
142 // a = b commutative_op a
143 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
145 hir::BiAdd | hir::BiMul |
146 hir::BiAnd | hir::BiOr |
147 hir::BiBitXor | hir::BiBitAnd | hir::BiBitOr => {