oxc-project/oxc · warning · OxcDiagnostic
Unexpected `void` operator
Error message
Unexpected `void` operator
What it means
Diagnostic from the `no-void` rule. The `void` operator evaluates its operand and discards the result, usually to obtain `undefined` or to mark an expression as intentionally unused (e.g. `void promise`). Because `undefined` is a global since ES5, the operator is confusing to readers, so oxlint flags `void expr` with help 'Use `undefined` instead'. The rule config `NoUselessEscape`-style struct exposes `allowAsStatement: bool` to permit `void` used purely as a statement.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_void.rs:17
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::UnaryOperator;
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
AstNode,
ast_util::outermost_paren_parent,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
fn no_void_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected `void` operator")
.with_help("Use `undefined` instead")
.with_label(span)
}
#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoVoid {
/// If set to `true`, using `void` as a standalone statement is allowed.
pub allow_as_statement: bool,
}
declare_oxc_lint!(
/// ### What it does
///
/// Disallows the use of the `void` operator.
///
/// ### Why is this bad?
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace value uses with `undefined`: `const u = void 0` -> `const u = undefined`.
- For discarding promises, configure `"allowAsStatement": true` and keep `void fetch(...)` in statement position, or use `.catch` / proper awaiting.
- In TypeScript, prefer `await` or explicit `void`-returning helpers over the operator.
- Disable the rule if your style guide embraces `void` for fire-and-forget semantics.
Example fix
// before const u = void 0; // after const u = undefined;
Defensive patterns
Strategy: validation
Validate before calling
const usesVoidOperator = /(?:^|[^.\w$])void\s/.test(sourceLine);
Prevention
- Use `undefined` instead of `void 0` for values.
- If you intentionally discard promises, set `allowAsStatement: true` in config.
- In TypeScript, satisfy no-floating-promises with real handling or typed helpers.
When it happens
Trigger: `const u = void 0;`, `void someAsyncFn();` as an expression, `() => void doWork()` arrow bodies. Fires on UnaryExpression with operator Void; when `allowAsStatement: true` is configured, statement-position voids are skipped and only expression uses (like `const x = void y`) are reported.
Common situations: Minifier-era idioms `void 0` copied into source; React/Angular code marking fire-and-forget promises; TypeScript code using `void` to satisfy no-floating-promises.
Related errors
- Avoid unnecessary use of .{name}()
- Unexpected var, use let or const instead.
- Unexpected use of `with` statement.
- Empty array binding pattern
- Empty object binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/249755a31db37185.
Report an issue: GitHub.