oxc-project/oxc · error · OxcDiagnostic

Use `Buffer.alloc()` or `Buffer.from()` instead of the depre

Error message

Use `Buffer.alloc()` or `Buffer.from()` instead of the deprecated `new Buffer()` constructor.

What it means

Raised by unicorn/no-new-buffer when the deprecated Node.js `new Buffer()` constructor is invoked (detected as a global reference in run()). The constructor is unsafe because it can initialize from uninitialized memory; the faulting input is the `new Buffer(...)` expression at the labeled span.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_new_buffer.rs:13

use oxc_ast::{
    AstKind,
    ast::{Argument, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::{GetSpan, Span};

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

fn no_new_buffer_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `Buffer.alloc()` or `Buffer.from()` instead of the deprecated `new Buffer()` constructor.")
        .with_help("`new Buffer()` is deprecated, use `Buffer.alloc()` or `Buffer.from()` instead.")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallows the deprecated `new Buffer()` constructor.
    ///
    /// ### Why is this bad?
    ///
    /// Enforces the use of [Buffer.from](https://nodejs.org/api/buffer.html#static-method-bufferfromarray) and [Buffer.alloc()](https://nodejs.org/api/buffer.html#static-method-bufferallocsize-fill-encoding) instead of [new Buffer()](https://nodejs.org/api/buffer.html#new-bufferarray), which has been deprecated since Node.js 4.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use `Buffer.alloc(size, fill)` to allocate initialized memory
  2. Use `Buffer.from(data)` to create a buffer from existing data
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/no_new_buffer.rs:13 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/da1b16c5eac243b3. Report an issue: GitHub.