Improved suggestion on misrefactored_assign_op lint. Fixes #1239

This commit is contained in:
flip1995 2018-01-30 14:58:38 +01:00
parent 39d1d6081f
commit b7cb0752ff
3 changed files with 60 additions and 13 deletions

View file

@ -87,19 +87,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
});
if let hir::ExprBinary(binop, ref l, ref r) = rhs.node {
if op.node == binop.node {
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
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.span))
(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, "..");
db.span_suggestion(
expr.span,
"replace it with",
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
&format!("Did you mean {} = {} {} {} or {} = {}? Consider replacing it with",
snip_a, snip_a, op.node.as_str(), snip_r,
snip_a, sugg::make_binop(higher::binop(op.node), a, r)),
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r)
);
},
);