oxc-project/oxc · warning · OxcDiagnostic

Found a useless array length check

Error message

Found a useless array length check

What it means

Diagnostic from the oxlint rule `unicorn/no-useless-length-check` (correctness category). This variant (the `some` diagnostic) fires when a logical `&&` chain contains both a non-empty length check on an array (`array.length !== 0` or `array.length > 0`) and an `array.some(...)` call on the same identifier. `Array#some()` already returns `false` for an empty array, so the non-empty check is redundant.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_useless_length_check.rs:15

use std::fmt::Debug;

use oxc_ast::{
    AstKind,
    ast::{Expression, LogicalExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::{BinaryOperator, LogicalOperator};

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

fn some(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Found a useless array length check")
        .with_help(
            "The non-empty check is useless as `Array#some()` returns `false` for an empty array.",
        )
        .with_label(span)
}

fn every(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Found a useless array length check")
        .with_help(
            "The empty check is useless as `Array#every()` returns `true` for an empty array.",
        )
        .with_label(span)
}

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

declare_oxc_lint!(

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Delete the length check: `if (array.length > 0 && array.some(fn))` -> `if (array.some(fn))`.
  2. If the two operands reference different arrays (`array1` vs `array2`), rename so it is clear they are intentionally different; the rule correctly skips those.
  3. Keep the check only when `.some` is optional-chained, since `array?.some(...)` short-circuits differently.
  4. Add an inline disable comment for deliberate cases.

Example fix

// before
if (array.length > 0 && array.some(isReady)) {
  run();
}

// after
if (array.some(isReady)) {
  run();
}
Defensive patterns

Strategy: validation

Validate before calling

# detect non-empty length checks paired with .some() in && chains
rg -n --type js -U '\.length\s*(?:!==|>)\s*0[\s\S]{0,80}&&[\s\S]{0,80}\.some\(' src/

Prevention

When it happens

Trigger: `array.length > 0 && array.some(Boolean)`, `array.length !== 0 && array.some(Boolean)`, either order (`array.some(Boolean) && array.length > 0`), flattened chains (`foo && array.length !== 0 && bar && array.some(Boolean)`), and parenthesized operands. Requires the same array identifier on both sides, strict operators only (`!==`, `>`; not `!=`, `>=`), the literal `0` on the right, and a non-optional `.some` call (`array?.some(...)` is skipped).

Common situations: Guard clauses written defensively around `some`, often copy-pasted from code that used `for` loops or `.filter().length`. Appears when enabling the oxlint correctness category or unicorn plugin.

Related errors


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