Skip to content

Commit

Permalink
fix(linter) fix false positive for erasing-op in 0/0 case
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Jan 12, 2024
1 parent 712e99c commit d699f7a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/oxc_linter/src/rules/oxc/erasing_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl Rule for ErasingOp {
check_op(binary_expression, &binary_expression.right, ctx);
}
BinaryOperator::Division => {
if is_number_value(&binary_expression.right, 0.0) {
return;
}
check_op(binary_expression, &binary_expression.left, ctx);
}
_ => (),
Expand All @@ -66,7 +69,7 @@ impl Rule for ErasingOp {
}

fn is_number_value(expr: &Expression, value: f64) -> bool {
if let Expression::NumberLiteral(number_literal) = expr {
if let Expression::NumberLiteral(number_literal) = expr.without_parenthesized() {
(number_literal.value - value).abs() < f64::EPSILON
} else {
false
Expand All @@ -87,7 +90,7 @@ fn check_op<'a, 'b>(
fn test() {
use crate::tester::Tester;

let pass = vec!["x * 1;", "1 * x;", "5 & x;", "x / 1;", "1 / x;"];
let pass = vec!["x * 1;", "1 * x;", "5 & x;", "x / 1;", "1 / x;", "0 / 0"];

let fail = vec!["x * 0;", "0 * x;", "0 & x;", "0 / x;"];

Expand Down

0 comments on commit d699f7a

Please sign in to comment.