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.
///
/// ### ExamplesView on GitHub (pinned to a3d33dda7c)
Solutions
- Replace the whole expression with `set.size`.
- 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.
- 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
- Memorize the Set/Map accessors: `.size` (not `.length`), `.has` (not `.includes`).
- When converting array code to Set code, grep for `.length` on converted identifiers the same commit.
- Let `oxlint --fix` apply the rewrite; the fix is local and safe.
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
- The {expr_type} is useless
- Unnecessary `.getTime()` call
- Prefer `{} {}` over `{} {}` to check {}.
- Invalid escape sequence in template literal.
- No spaces inside empty pair of braces allowed
AI-assisted analysis of oxc-project/oxc@a3d33dda7c (2026-08-20).
Data as JSON: /api/errors/75dc694988b5d092.
Report an issue: GitHub.