Refactor suggested by krishna-veerareddy
This commit is contained in:
parent
0a6d299409
commit
d88750371d
1 changed files with 65 additions and 84 deletions
|
@ -420,103 +420,84 @@ fn is_zero(expr: &Expr<'_>) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If the expressions are not opposites, return None
|
||||||
|
/// 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(
|
||||||
|
Block {
|
||||||
|
stmts: [],
|
||||||
|
expr: Some(expr2_inner),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
_,
|
||||||
|
) = &expr2.kind
|
||||||
|
{
|
||||||
|
if let ExprKind::Unary(UnOp::UnNeg, expr1_neg) = &expr1_inner.kind {
|
||||||
|
if are_exprs_equal(cx, expr1_neg, expr2_inner) {
|
||||||
|
return Some((false, expr2_inner));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let ExprKind::Unary(UnOp::UnNeg, expr2_neg) = &expr2_inner.kind {
|
||||||
|
if are_exprs_equal(cx, expr1_inner, expr2_neg) {
|
||||||
|
return Some((true, expr1_inner));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
|
fn check_custom_abs(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
|
||||||
if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) {
|
if let Some((cond, body, Some(else_body))) = higher::if_block(&expr) {
|
||||||
if let ExprKind::Block(
|
if let Some((expr1_pos, body)) = are_opposites(cx, body, else_body) {
|
||||||
Block {
|
let pos_abs_sugg = (
|
||||||
stmts: [],
|
|
||||||
expr:
|
|
||||||
Some(Expr {
|
|
||||||
kind: ExprKind::Unary(UnOp::UnNeg, else_expr),
|
|
||||||
..
|
|
||||||
}),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
_,
|
|
||||||
) = else_body.kind
|
|
||||||
{
|
|
||||||
if let ExprKind::Block(
|
|
||||||
Block {
|
|
||||||
stmts: [],
|
|
||||||
expr: Some(body),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
_,
|
|
||||||
) = &body.kind
|
|
||||||
{
|
|
||||||
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",
|
"This looks like you've implemented your own absolute value function",
|
||||||
"try",
|
|
||||||
format!("{}.abs()", Sugg::hir(cx, body, "..")),
|
format!("{}.abs()", Sugg::hir(cx, body, "..")),
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
);
|
||||||
|
let neg_abs_sugg = (
|
||||||
|
"This looks like you've implemented your own negative absolute value function",
|
||||||
|
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
|
||||||
|
);
|
||||||
|
let sugg = if is_testing_positive(cx, cond, body) {
|
||||||
|
if expr1_pos {
|
||||||
|
pos_abs_sugg
|
||||||
|
} else {
|
||||||
|
neg_abs_sugg
|
||||||
|
}
|
||||||
} else if is_testing_negative(cx, cond, body) {
|
} else if is_testing_negative(cx, cond, body) {
|
||||||
|
if expr1_pos {
|
||||||
|
neg_abs_sugg
|
||||||
|
} else {
|
||||||
|
pos_abs_sugg
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
SUBOPTIMAL_FLOPS,
|
SUBOPTIMAL_FLOPS,
|
||||||
expr.span,
|
expr.span,
|
||||||
"This looks like you've implemented your own negative absolute value function",
|
sugg.0,
|
||||||
"try",
|
"try",
|
||||||
format!("-{}.abs()", Sugg::hir(cx, body, "..")),
|
sugg.1,
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
if let ExprKind::Block(
|
|
||||||
Block {
|
|
||||||
stmts: [],
|
|
||||||
expr:
|
|
||||||
Some(Expr {
|
|
||||||
kind: ExprKind::Unary(UnOp::UnNeg, else_expr),
|
|
||||||
..
|
|
||||||
}),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
_,
|
|
||||||
) = &body.kind
|
|
||||||
{
|
|
||||||
if let ExprKind::Block(
|
|
||||||
Block {
|
|
||||||
stmts: [],
|
|
||||||
expr: Some(body),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
_,
|
|
||||||
) = &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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FloatingPointArithmetic {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue