fix(linter): fix false positive for erasing-op in 0/0 case (#2009)

This commit is contained in:
Cameron 2024-01-13 02:32:26 +00:00 committed by GitHub
parent 712e99cf9b
commit 107a32ea48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
}
_ => (),
@ -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
@ -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;"];