Skip to content

Commit f69351e

Browse files
authored
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.
2 parents 1902384 + 9494f22 commit f69351e

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

clippy_lints/src/assign_ops.rs

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -70,52 +70,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
7070
match &expr.node {
7171
hir::ExprKind::AssignOp(op, lhs, rhs) => {
7272
if let hir::ExprKind::Binary(binop, l, r) = &rhs.node {
73-
if op.node == binop.node {
74-
let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
75-
span_lint_and_then(
76-
cx,
77-
MISREFACTORED_ASSIGN_OP,
78-
expr.span,
79-
"variable appears on both sides of an assignment operation",
80-
|db| {
81-
if let (Some(snip_a), Some(snip_r)) =
82-
(snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span))
83-
{
84-
let a = &sugg::Sugg::hir(cx, assignee, "..");
85-
let r = &sugg::Sugg::hir(cx, rhs, "..");
86-
let long =
87-
format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
88-
db.span_suggestion(
89-
expr.span,
90-
&format!(
91-
"Did you mean {} = {} {} {} or {}? Consider replacing it with",
92-
snip_a,
93-
snip_a,
94-
op.node.as_str(),
95-
snip_r,
96-
long
97-
),
98-
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
99-
Applicability::MachineApplicable,
100-
);
101-
db.span_suggestion(
102-
expr.span,
103-
"or",
104-
long,
105-
Applicability::MachineApplicable, // snippet
106-
);
107-
}
108-
},
109-
);
110-
};
111-
// lhs op= l op r
112-
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
113-
lint(lhs, r);
114-
}
115-
// lhs op= l commutative_op r
116-
if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
117-
lint(lhs, l);
118-
}
73+
if op.node != binop.node {
74+
return;
75+
}
76+
// lhs op= l op r
77+
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
78+
lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, r);
79+
}
80+
// lhs op= l commutative_op r
81+
if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
82+
lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, l);
11983
}
12084
}
12185
},
@@ -231,6 +195,48 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
231195
}
232196
}
233197

198+
fn lint_misrefactored_assign_op(
199+
cx: &LateContext<'_, '_>,
200+
expr: &hir::Expr,
201+
op: hir::BinOp,
202+
rhs: &hir::Expr,
203+
assignee: &hir::Expr,
204+
rhs_other: &hir::Expr,
205+
) {
206+
span_lint_and_then(
207+
cx,
208+
MISREFACTORED_ASSIGN_OP,
209+
expr.span,
210+
"variable appears on both sides of an assignment operation",
211+
|db| {
212+
if let (Some(snip_a), Some(snip_r)) = (snippet_opt(cx, assignee.span), snippet_opt(cx, rhs_other.span)) {
213+
let a = &sugg::Sugg::hir(cx, assignee, "..");
214+
let r = &sugg::Sugg::hir(cx, rhs, "..");
215+
let long = format!("{} = {}", snip_a, sugg::make_binop(higher::binop(op.node), a, r));
216+
db.span_suggestion(
217+
expr.span,
218+
&format!(
219+
"Did you mean {} = {} {} {} or {}? Consider replacing it with",
220+
snip_a,
221+
snip_a,
222+
op.node.as_str(),
223+
snip_r,
224+
long
225+
),
226+
format!("{} {}= {}", snip_a, op.node.as_str(), snip_r),
227+
Applicability::MachineApplicable,
228+
);
229+
db.span_suggestion(
230+
expr.span,
231+
"or",
232+
long,
233+
Applicability::MachineApplicable, // snippet
234+
);
235+
}
236+
},
237+
);
238+
}
239+
234240
fn is_commutative(op: hir::BinOpKind) -> bool {
235241
use rustc::hir::BinOpKind::*;
236242
match op {

0 commit comments

Comments
 (0)