oxc-project/oxc · error · OxcDiagnostic

Do not use a reference value as the fill value.

Error message

Do not use a reference value as the fill value.

What it means

Raised by unicorn/no-array-fill-with-reference-type when `Array.prototype.fill` is called with an argument that is a reference type (object, array, function), so every slot aliases the same instance. The faulting input is the `.fill(referenceValue)` call at the labeled span.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_array_fill_with_reference_type.rs:19

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

use crate::{
    AstNode,
    ast_util::variable_declaration_kind,
    ast_util::{get_declaration_of_variable, is_method_call},
    context::LintContext,
    rule::Rule,
    utils::is_regexp_callee,
};

fn no_array_fill_with_reference_type_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Do not use a reference value as the fill value.")
        .with_help("Use `Array.from({ length: n }, () => /* new value */)` or `.map(() => /* new value */)` to create a distinct value for each element.")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallows using reference values as `Array#fill()` values.
    ///
    /// ### Why is this bad?
    ///
    /// `Array#fill()` reuses the same value for every array element. When the
    /// fill value is an object, array, class, or most constructed objects, all
    /// elements point at the same reference and mutating one element mutates the
    /// shared value observed by the others.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use `Array.from({ length: n }, () => createValue())` to create a fresh value per element
  2. Fill with a primitive and populate objects individually
  3. Map over the filled array to clone the reference into each slot
Defensive patterns

Strategy: validation

When it happens

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