oxc-project/oxc · error · OxcDiagnostic

Prefer `Math.trunc()` over instead of `{bad_op} 0`.

Error message

Prefer `Math.trunc()` over instead of `{bad_op} 0`.

What it means

Diagnostic from the unicorn/prefer-math-trunc lint rule. It fires when an integer truncation is performed via a bitwise operator (`x | 0`, `x >> 0`, `x ^ 0`, `~~x`, or the assignment forms) instead of `Math.trunc(x)`. Bitwise truncation silently fails (returns 0) for values outside the 32-bit range, while `Math.trunc` is correct for all numbers. The message names the offending operator. It is a correctness/style lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_math_trunc.rs:20

    AstKind,
    ast::{Expression, IdentifierReference},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_ecmascript::{
    GlobalContext,
    side_effects::{MayHaveSideEffects, MayHaveSideEffectsContext, PropertyReadSideEffects},
};
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};

use crate::{
    AstNode, context::LintContext, fixer::RuleFixer, rule::Rule, utils::pad_fix_with_token_boundary,
};

fn prefer_math_trunc_diagnostic(span: Span, bad_op: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Prefer `Math.trunc()` over instead of `{bad_op} 0`."))
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferMathTrunc;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prefers use of [`Math.trunc()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc) instead of bitwise operations for clarity and more reliable results.
    ///
    /// It prevents the use of the following bitwise operations:
    /// - `x | 0` ([`bitwise OR`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR) with 0)
    /// - `~~x` (two [`bitwise NOT`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT))
    /// - `x >> 0` ([`Signed Right Shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift) with 0)
    /// - `x << 0` ([`Left Shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift) with 0)
    /// - `x ^ 0` ([`bitwise XOR Shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR) with 0)
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace `x | 0`, `x >> 0`, `x ^ 0`, or `~~x` with `Math.trunc(x)`
  2. Replace the corresponding compound assignment (`x |= 0` etc.) with `x = Math.trunc(x)`
  3. Apply the rule's auto-fix
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_math_trunc.rs:20 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/954d67048b966dab. Report an issue: GitHub.