oxc-project/oxc · error · OxcDiagnostic

Don't use `Array#reduce()` and `Array#reduceRight()`, use `f

Error message

Don't use `Array#reduce()` and `Array#reduceRight()`, use `for` loops instead.

What it means

Raised by unicorn/no-array-reduce when `Array#reduce()` or `Array#reduceRight()` is used (prototype or instance call detected via is_method_call/is_prototype_property). The faulting input is the reduce call at the labeled span; the report is stylistic/complexity-driven, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_array_reduce.rs:20

    AstKind,
    ast::{Argument, CallExpression, Expression, Statement},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde::Deserialize;

use crate::{
    AstNode,
    ast_util::is_method_call,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
    utils::is_prototype_property,
};

fn no_array_reduce_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(
        "Don't use `Array#reduce()` and `Array#reduceRight()`, use `for` loops instead.",
    )
    .with_help("Refactor your code to use `for` loops instead.")
    .with_label(span)
}

#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoArrayReduce {
    /// When set to `true`, allows simple operations (like summing numbers) in `reduce` and `reduceRight` calls.
    pub allow_simple_operations: bool,
}

impl Default for NoArrayReduce {
    fn default() -> Self {
        Self { allow_simple_operations: true }
    }
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite the reduction as a `for…of` loop accumulating into a named variable
  2. Use more specific combinators (map/filter/flatMap/some/every) when they express the intent
  3. Disable the rule for the rare genuinely reduce-shaped case
Defensive patterns

Strategy: validation

When it happens

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