{"record":{"id":"954d67048b966dab","repo":"oxc-project/oxc","slug":"prefer-math-trunc-over-instead-of-bad-op-0","errorCode":null,"errorMessage":"Prefer `Math.trunc()` over instead of `{bad_op} 0`.","messagePattern":"Prefer `Math\\.trunc\\(\\)` over instead of `(.+?) 0`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_math_trunc.rs","lineNumber":20,"sourceCode":"    AstKind,\n    ast::{Expression, IdentifierReference},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_ecmascript::{\n    GlobalContext,\n    side_effects::{MayHaveSideEffects, MayHaveSideEffectsContext, PropertyReadSideEffects},\n};\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::IsGlobalReference;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};\n\nuse crate::{\n    AstNode, context::LintContext, fixer::RuleFixer, rule::Rule, utils::pad_fix_with_token_boundary,\n};\n\nfn prefer_math_trunc_diagnostic(span: Span, bad_op: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer `Math.trunc()` over instead of `{bad_op} 0`.\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferMathTrunc;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// 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.\n    ///\n    /// It prevents the use of the following bitwise operations:\n    /// - `x | 0` ([`bitwise OR`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR) with 0)\n    /// - `~~x` (two [`bitwise NOT`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT))\n    /// - `x >> 0` ([`Signed Right Shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift) with 0)\n    /// - `x << 0` ([`Left Shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift) with 0)\n    /// - `x ^ 0` ([`bitwise XOR Shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR) with 0)\n    ///","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_math_trunc.rs#L2-L38","documentation":"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.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_math_trunc.rs:20 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace `x | 0`, `x >> 0`, `x ^ 0`, or `~~x` with `Math.trunc(x)`","Replace the corresponding compound assignment (`x |= 0` etc.) with `x = Math.trunc(x)`","Apply the rule's auto-fix"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}