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, ASSIGN_OPS, expr.span, "assign operation detected", |db| {
56 hir::ExprBinary(op2, _, _) if op2 != op => {
57 db.span_suggestion(expr.span,
59 format!("{} = {} {} ({})", l, l, op.node.as_str(), r));
62 db.span_suggestion(expr.span,
64 format!("{} = {} {} {}", l, l, op.node.as_str(), r));
69 span_lint(cx, ASSIGN_OPS, expr.span, "assign operation detected");
72 hir::ExprAssign(ref assignee, ref e) => {
73 if let hir::ExprBinary(op, ref l, ref r) = e.node {
74 let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
75 let ty = cx.tcx.expr_ty(assignee);
76 if ty.walk_shallow().next().is_some() {
77 return; // implements_trait does not work with generics
79 let rty = cx.tcx.expr_ty(rhs);
80 if rty.walk_shallow().next().is_some() {
81 return; // implements_trait does not work with generics
84 ($op:expr, $cx:expr, $ty:expr, $rty:expr, $($trait_name:ident:$full_trait_name:ident),+) => {
86 $(hir::$full_trait_name => {
87 let [krate, module] = ::utils::paths::OPS_MODULE;
88 let path = [krate, module, concat!(stringify!($trait_name), "Assign")];
89 let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
92 return; // useless if the trait doesn't exist
94 implements_trait($cx, $ty, trait_id, vec![$rty])
116 if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span),
117 snippet_opt(cx, rhs.span)) {
118 span_lint_and_then(cx,
121 "manual implementation of an assign operation",
123 db.span_suggestion(expr.span,
125 format!("{} {}= {}", snip_a, op.node.as_str(), snip_r));
131 "manual implementation of an assign operation");
136 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
139 // a = b commutative_op a
140 if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
142 hir::BiAdd | hir::BiMul | hir::BiAnd | hir::BiOr | hir::BiBitXor | hir::BiBitAnd |