oxc-project/oxc · error · OxcDiagnostic
Prefer `Math.hypot(…)` over alternatives
Error message
Prefer `Math.hypot(…)` over alternatives
What it means
Diagnostic from the unicorn/prefer-modern-math-apis lint rule. It fires when a sum-of-squares is square-rooted manually, e.g. `Math.sqrt(a * a + b * b)` for Euclidean distance; `Math.hypot(a, b)` computes the same result with better overflow/underflow behavior. The flagged input is the sqrt-of-squared-sum expression. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:19
use oxc_ast::{
AstKind,
ast::{Argument, BinaryExpression, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::BinaryOperator;
use crate::{
AstNode, ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression,
};
fn prefer_math_abs(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `Math.abs(x)` over alternatives").with_label(span)
}
fn prefer_math_hypot(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `Math.hypot(…)` over alternatives").with_label(span)
}
fn prefer_math_log_n(span: Span, good_method: &str, bad_method: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Prefer `Math.{good_method}(x)` over `{bad_method}`"))
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferModernMathApis;
declare_oxc_lint!(
/// ### What it does
///
/// Checks for usage of legacy patterns for mathematical operations.
///
/// ### Why is this bad?
///
/// Modern JavaScript provides more concise and readable alternatives to legacy patterns.View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `Math.sqrt(a*a + b*b)` with `Math.hypot(a, b)`
- Pass all dimensions as arguments for N-dimensional distances
- Apply the rule's auto-fix
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:19 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/ac4fa87724713007.
Report an issue: GitHub.