diff options
| author | Philipp Krones <hello@philkrones.com> | 2019-03-09 12:24:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-09 12:24:43 +0100 |
| commit | f69351e9957d44711d0decabccfb925bc998de70 (patch) | |
| tree | 30e8e78b449b9b59f3b7cea433c603be29b1849c | |
| parent | 1902384d153407ff04d2048812c8b9eb9f754f74 (diff) | |
| parent | 9494f22f06f29ed1c47314b10ed62cfc1d4aff80 (diff) | |
| download | rust-f69351e9957d44711d0decabccfb925bc998de70.tar.gz rust-f69351e9957d44711d0decabccfb925bc998de70.zip | |
Rollup merge of #3852 - phansch:refactor_assign_ops, r=flip1995
Refactor: Cleanup one part of assign_ops lint Removes a lot of indentation and separates lint emission from lint logic. Only touches the `hir::ExprKind::AssignOp` part of the lint.
| -rw-r--r-- | clippy_lints/src/assign_ops.rs | 98 |
1 files changed, 52 insertions, 46 deletions
diff --git a/clippy_lints/src/assign_ops.rs b/clippy_lints/src/assign_ops.rs index 87e92c2bd24..9e0b87bc377 100644 --- a/clippy_lints/src/assign_ops.rs +++ b/clippy_lints/src/assign_ops.rs @@ -70,52 +70,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps { match &expr.node { hir::ExprKind::AssignOp(op, lhs, rhs) => { if let hir::ExprKind::Binary(binop, l, r) = &rhs.node { - if op.node == binop.node { - let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| { - span_lint_and_then( - cx, - MISREFACTORED_ASSIGN_OP, - expr.span, - "variable appears on both sides of an assignment operation", - |db| { - if let (Some(snip_a), Some(snip_r)) = - (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span)) - { - let a = &sugg::Sugg::hir(cx, assignee, ".."); - let r = &sugg::Sugg::hir(cx, rhs, ".."); - let long = - format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r)); - db.span_suggestion( - expr.span, - &format!( - "Did you mean {} = {} {} {} or {}? Consider replacing it with", - snip_a, - snip_a, - op.node.as_str(), - snip_r, - long - ), - format!("{} {}= {}", snip_a, op.node.as_str(), snip_r), - Applicability::MachineApplicable, - ); - db.span_suggestion( - expr.span, - "or", - long, - Applicability::MachineApplicable, // snippet - ); - } - }, - ); - }; - // lhs op= l op r - if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) { - lint(lhs, r); - } - // lhs op= l commutative_op r - if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) { - lint(lhs, l); - } + if op.node != binop.node { + return; + } + // lhs op= l op r + if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) { + lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, r); + } + // lhs op= l commutative_op r + if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) { + lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, l); } } }, @@ -231,6 +195,48 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps { } } +fn lint_misrefactored_assign_op( + cx: &LateContext<'_, '_>, + expr: &hir::Expr, + op: hir::BinOp, + rhs: &hir::Expr, + assignee: &hir::Expr, + rhs_other: &hir::Expr, +) { + span_lint_and_then( + cx, + MISREFACTORED_ASSIGN_OP, + expr.span, + "variable appears on both sides of an assignment operation", + |db| { + if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span)) { + let a = &sugg::Sugg::hir(cx, assignee, ".."); + let r = &sugg::Sugg::hir(cx, rhs, ".."); + let long = format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r)); + db.span_suggestion( + expr.span, + &format!( + "Did you mean {} = {} {} {} or {}? Consider replacing it with", + snip_a, + snip_a, + op.node.as_str(), + snip_r, + long + ), + format!("{} {}= {}", snip_a, op.node.as_str(), snip_r), + Applicability::MachineApplicable, + ); + db.span_suggestion( + expr.span, + "or", + long, + Applicability::MachineApplicable, // snippet + ); + } + }, + ); +} + fn is_commutative(op: hir::BinOpKind) -> bool { use rustc::hir::BinOpKind::*; match op { |
