|
| 1 | +use rustc::hir; |
| 2 | +use rustc::lint::*; |
| 3 | +use utils::{span_lint_and_then, span_lint, snippet_opt, SpanlessEq, get_trait_def_id, implements_trait}; |
| 4 | + |
| 5 | +/// **What it does:** This lint checks for `+=` operations and similar |
| 6 | +/// |
| 7 | +/// **Why is this bad?** Projects with many developers from languages without those operations |
| 8 | +/// may find them unreadable and not worth their weight |
| 9 | +/// |
| 10 | +/// **Known problems:** Types implementing `OpAssign` don't necessarily implement `Op` |
| 11 | +/// |
| 12 | +/// **Example:** |
| 13 | +/// ``` |
| 14 | +/// a += 1; |
| 15 | +/// ``` |
| 16 | +declare_restriction_lint! { |
| 17 | + pub ASSIGN_OPS, |
| 18 | + "Any assignment operation" |
| 19 | +} |
| 20 | + |
| 21 | +/// **What it does:** Check for `a = a op b` or `a = b commutative_op a` patterns |
| 22 | +/// |
| 23 | +/// **Why is this bad?** These can be written as the shorter `a op= b` |
| 24 | +/// |
| 25 | +/// **Known problems:** While forbidden by the spec, `OpAssign` traits may have implementations that differ from the regular `Op` impl |
| 26 | +/// |
| 27 | +/// **Example:** |
| 28 | +/// |
| 29 | +/// ``` |
| 30 | +/// let mut a = 5; |
| 31 | +/// ... |
| 32 | +/// a = a + b; |
| 33 | +/// ``` |
| 34 | +declare_lint! { |
| 35 | + pub ASSIGN_OP_PATTERN, |
| 36 | + Warn, |
| 37 | + "assigning the result of an operation on a variable to that same variable" |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Copy, Clone, Default)] |
| 41 | +pub struct AssignOps; |
| 42 | + |
| 43 | +impl LintPass for AssignOps { |
| 44 | + fn get_lints(&self) -> LintArray { |
| 45 | + lint_array!(ASSIGN_OPS, ASSIGN_OP_PATTERN) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl LateLintPass for AssignOps { |
| 50 | + fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) { |
| 51 | + match expr.node { |
| 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, |
| 55 | + ASSIGN_OPS, |
| 56 | + expr.span, |
| 57 | + "assign operation detected", |
| 58 | + |db| { |
| 59 | + match rhs.node { |
| 60 | + hir::ExprBinary(op2, _, _) if op2 != op => { |
| 61 | + db.span_suggestion(expr.span, |
| 62 | + "replace it with", |
| 63 | + format!("{} = {} {} ({})", l, l, op.node.as_str(), r)); |
| 64 | + }, |
| 65 | + _ => { |
| 66 | + db.span_suggestion(expr.span, |
| 67 | + "replace it with", |
| 68 | + format!("{} = {} {} {}", l, l, op.node.as_str(), r)); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + } else { |
| 73 | + span_lint(cx, |
| 74 | + ASSIGN_OPS, |
| 75 | + expr.span, |
| 76 | + "assign operation detected"); |
| 77 | + } |
| 78 | + }, |
| 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 |
| 85 | + } |
| 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 |
| 89 | + } |
| 90 | + macro_rules! ops { |
| 91 | + ($op:expr, $cx:expr, $ty:expr, $rty:expr, $($trait_name:ident:$full_trait_name:ident),+) => { |
| 92 | + match $op { |
| 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) { |
| 97 | + trait_id |
| 98 | + } else { |
| 99 | + return; // useless if the trait doesn't exist |
| 100 | + }; |
| 101 | + implements_trait($cx, $ty, trait_id, vec![$rty]) |
| 102 | + },)* |
| 103 | + _ => false, |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if ops!(op.node, cx, ty, rty, Add:BiAdd, |
| 108 | + Sub:BiSub, |
| 109 | + Mul:BiMul, |
| 110 | + Div:BiDiv, |
| 111 | + Rem:BiRem, |
| 112 | + And:BiAnd, |
| 113 | + Or:BiOr, |
| 114 | + BitAnd:BiBitAnd, |
| 115 | + BitOr:BiBitOr, |
| 116 | + BitXor:BiBitXor, |
| 117 | + Shr:BiShr, |
| 118 | + Shl:BiShl |
| 119 | + ) { |
| 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, |
| 122 | + ASSIGN_OP_PATTERN, |
| 123 | + expr.span, |
| 124 | + "manual implementation of an assign operation", |
| 125 | + |db| { |
| 126 | + db.span_suggestion(expr.span, |
| 127 | + "replace it with", |
| 128 | + format!("{} {}= {}", snip_a, op.node.as_str(), snip_r)); |
| 129 | + }); |
| 130 | + } else { |
| 131 | + span_lint(cx, |
| 132 | + ASSIGN_OP_PATTERN, |
| 133 | + expr.span, |
| 134 | + "manual implementation of an assign operation"); |
| 135 | + } |
| 136 | + } |
| 137 | + }; |
| 138 | + // a = a op b |
| 139 | + if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) { |
| 140 | + lint(assignee, r); |
| 141 | + } |
| 142 | + // a = b commutative_op a |
| 143 | + if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) { |
| 144 | + match op.node { |
| 145 | + hir::BiAdd | hir::BiMul | |
| 146 | + hir::BiAnd | hir::BiOr | |
| 147 | + hir::BiBitXor | hir::BiBitAnd | hir::BiBitOr => { |
| 148 | + lint(assignee, l); |
| 149 | + }, |
| 150 | + _ => {}, |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + }, |
| 155 | + _ => {}, |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments