1
Fork 0

Refactor suggested by krishna-veerareddy

This commit is contained in:
JarredAllen 2020-03-01 22:37:37 -08:00
parent 0a6d299409
commit d88750371d

View file

@ -420,101 +420,82 @@ fn is_zero(expr: &Expr<'_>) -> bool {
} }
} }
fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) { /// If the expressions are not opposites, return None
if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) { /// Otherwise, return true if expr2 = -expr1, false if expr1 = -expr2 and return the positive
/// expression
fn are_opposites<'a>(
cx: &LateContext<'_, '_>,
expr1: &'a Expr<'a>,
expr2: &'a Expr<'a>,
) -> Option<(bool, &'a Expr<'a>)> {
if let ExprKind::Block(
Block {
stmts: [],
expr: Some(expr1_inner),
..
},
_,
) = &expr1.kind
{
if let ExprKind::Block( if let ExprKind::Block(
Block { Block {
stmts: [], stmts: [],
expr: expr: Some(expr2_inner),
Some(Expr {
kind: ExprKind::Unary(UnOp::UnNeg, else_expr),
..
}),
.. ..
}, },
_, _,
) = else_body.kind ) = &expr2.kind
{ {
if let ExprKind::Block( if let ExprKind::Unary(UnOp::UnNeg, expr1_neg) = &expr1_inner.kind {
Block { if are_exprs_equal(cx, expr1_neg, expr2_inner) {
stmts: [], return Some((false, expr2_inner));
expr: Some(body), }
.. }
}, if let ExprKind::Unary(UnOp::UnNeg, expr2_neg) = &expr2_inner.kind {
_, if are_exprs_equal(cx, expr1_inner, expr2_neg) {
) = &body.kind return Some((true, expr1_inner));
{
if are_exprs_equal(cx, else_expr, body) {
if is_testing_positive(cx, cond, body) {
span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
"This looks like you've implemented your own absolute value function",
"try",
format!("{}.abs()", Sugg::hir(cx, body, "..")),
Applicability::MachineApplicable,
);
} else if is_testing_negative(cx, cond, body) {
span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
"This looks like you've implemented your own negative absolute value function",
"try",
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
Applicability::MachineApplicable,
);
}
} }
} }
} }
if let ExprKind::Block( }
Block { None
stmts: [], }
expr:
Some(Expr { fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
kind: ExprKind::Unary(UnOp::UnNeg, else_expr), if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) {
.. if let Some((expr1_pos, body)) = are_opposites(cx, body, else_body) {
}), let pos_abs_sugg = (
.. "This looks like you've implemented your own absolute value function",
}, format!("{}.abs()", Sugg::hir(cx, body, "..")),
_, );
) = &body.kind let neg_abs_sugg = (
{ "This looks like you've implemented your own negative absolute value function",
if let ExprKind::Block( format!("-{}.abs()", Sugg::hir(cx, body, "..")),
Block { );
stmts: [], let sugg = if is_testing_positive(cx, cond, body) {
expr: Some(body), if expr1_pos {
.. pos_abs_sugg
}, } else {
_, neg_abs_sugg
) = &else_body.kind
{
if are_exprs_equal(cx, else_expr, body) {
if is_testing_negative(cx, cond, body) {
span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
"This looks like you've implemented your own absolute value function",
"try",
format!("{}.abs()", Sugg::hir(cx, body, "..")),
Applicability::MachineApplicable,
);
} else if is_testing_positive(cx, cond, body) {
span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
"This looks like you've implemented your own negative absolute value function",
"try",
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
Applicability::MachineApplicable,
);
}
} }
} } else if is_testing_negative(cx, cond, body) {
if expr1_pos {
neg_abs_sugg
} else {
pos_abs_sugg
}
} else {
return;
};
span_lint_and_sugg(
cx,
SUBOPTIMAL_FLOPS,
expr.span,
sugg.0,
"try",
sugg.1,
Applicability::MachineApplicable,
);
} }
} }
} }