Skip to content

Commit 866c895

Browse files
committed
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.
1 parent 00baf7a commit 866c895

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

clippy_lints/src/assign_ops.rs

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

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

0 commit comments

Comments
 (0)