oxc-project/oxc · warning · OxcDiagnostic

Use `Set#size` instead of converting a `Set` to an array and

Error message

Use `Set#size` instead of converting a `Set` to an array and using its `length` property.

What it means

This is the oxlint rule `unicorn/prefer-set-size`. It flags code that converts a `Set` to an array solely to read its `length` (e.g. `[...set].length` or `Array.from(set).length`) when the `Set` already exposes its element count as `Set#size`. The rule uses `get_declaration_of_variable` to trace the receiver back to a Set-producing expression before reporting.

Source

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

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

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

fn prefer_set_size_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(
        "Use `Set#size` instead of converting a `Set` to an array and using its `length` property.",
    )
    .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prefer `Set#size` over `Set#length` when the `Set` is converted to an array.
    ///
    /// ### Why is this bad?
    ///
    /// Using `Set#size` is more readable and performant.
    ///
    /// ### Examples

View on GitHub (pinned to a3d33dda7c)

Solutions

  1. Replace the whole expression with `set.size`.
  2. If the array conversion is needed for other reasons (logging, iteration), read the count from the Set separately instead of chaining `.length` on the conversion.
  3. Apply the provided fix via `oxlint --fix`, which rewrites the span to `set.size`.

Example fix

// before
const count = [...uniqueIds].length;

// after
const count = uniqueIds.size;
Defensive patterns

Strategy: validation

Validate before calling

// Read sizes from the source structure directly
const n = uniqueIds.size; // not [...uniqueIds].length
console.assert(Number.isInteger(n), 'size must be an integer');

Prevention

When it happens

Trigger: `[...set].length`, `Array.from(set).length`, or the same pattern through an intermediate variable declared as `[...set]` / `Array.from(set)` whose only use is `.length`. The rule recognizes the method call via `is_method_call` and checks the declaration with `get_declaration_of_variable`.

Common situations: Code written before `Set#size` was widely known, or converted from arrays to Sets without updating count reads; also snapshot/telemetry code like `expect(Array.from(ids).length).toBe(3)`.

Related errors


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