oxc-project/oxc · error · OxcDiagnostic

Missing throw

Error message

Missing throw

What it means

Oxlint rule `oxc/missing-throw` flags a `new Error(...)` expression used as a bare statement: as an ExpressionStatement or inside a block-bodied arrow function. The error object is constructed and immediately discarded — nothing is thrown or returned — so the surrounding function silently continues on what was meant to be a failure path. Only the exact callee `Error` is checked, and an autofix inserts `throw ` before the expression.

Source

Thrown at crates/oxc_linter/src/rules/oxc/missing_throw.rs:9

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{AstNode, context::LintContext, rule::Rule};

fn missing_throw_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Missing throw")
        .with_help("The `throw` keyword seems to be missing in front of this 'new' expression")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Checks whether the `throw` keyword is missing in front of a `new` expression.
    ///
    /// ### Why is this bad?
    ///
    /// The `throw` keyword is required in front of a `new` expression to throw an error. Omitting it is usually a mistake.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Add `throw`: `throw new Error('boom')` (autofix inserts it)
  2. If the arrow should produce the error rather than throw it, return it instead
  3. Search the codebase for other bare `new Error(` statements — oxlint lists every occurrence

Example fix

// before
const fail = () => { new Error('nope'); };

// after
const fail = () => { throw new Error('nope'); };
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — correctness rule with autofix
{
  "rules": { "oxc/missing-throw": "error" }
}
// CLI (inserts `throw`): npx oxlint --fix src/

Prevention

When it happens

Trigger: `function foo() { new Error('boom') }`; `const fail = () => { new Error('nope') }`. Not flagged: `throw new Error()`, the expression-bodied `() => new Error()` (it returns the error), or `[new Error()]`.

Common situations: Deleting `throw` while editing; converting an arrow function from expression body to block body and dropping the throw; error-handling code written but never wired up, so failures pass silently.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/598ea44e5d0d5a34. Report an issue: GitHub.