oxc-project/oxc · warning · OxcDiagnostic
The {expr_type} is useless
Error message
The {expr_type} is useless What it means
Diagnostic from the oxlint rule `unicorn/no-useless-collection-argument`. It fires when `new Set`, `new Map`, `new WeakSet`, or `new WeakMap` is constructed with exactly one argument that is an empty array, an empty string, `null`, or `undefined`, or a `foo ?? <such value>` coalesce fallback. These constructors accept nullish input, so the argument or fallback does nothing. The message fills `{expr_type}` with 'empty array', 'empty string', 'null', or 'undefined', and a suggestion fix removes the useless code.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_useless_collection_argument.rs:20
AstKind,
ast::{Expression, LogicalExpression, NewExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{
AstNode,
ast_util::is_new_expression,
context::LintContext,
fixer::{RuleFix, RuleFixer},
rule::Rule,
utils::is_empty_array_expression,
};
fn no_useless_collection_argument_diagnostic(span: Span, expr_type: &str) -> OxcDiagnostic {
let message = format!("The {expr_type} is useless");
OxcDiagnostic::warn(message).with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoUselessCollectionArgument;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow useless values or fallbacks in `Set`, `Map`, `WeakSet`, or `WeakMap`.
///
/// ### Why is this bad?
///
/// It is unnecessary to pass an empty array or empty string when
/// constructing a `Set`, `Map`, `WeakSet`, or `WeakMap`, since
/// they accept nullish values.
///
/// It is also unnecessary to provide a fallback for possible nullish values.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Remove the useless argument or fallback: `new Set(foo ?? [])` -> `new Set(foo)`, `new Set([])` -> `new Set()`.
- Apply the rule's suggestion fix via `oxlint --fix-suggestions` or your editor quick fix.
- If you need default entries, keep a non-empty fallback: `new Set(foo ?? [defaultItem])`.
- Keep `|| []` when you truly want an empty collection for every falsy value; the rule only flags `??` fallbacks.
Example fix
// before const set = new Set(foo ?? []); // after const set = new Set(foo);
Defensive patterns
Strategy: validation
Validate before calling
# detect useless Set/Map/WeakSet/WeakMap arguments before linting rg -n --type js 'new (?:Weak)?(?:Set|Map)\(\s*(?:\[\s*\]|\'\'|""|null|undefined|[^)]*\?\?\s*(?:\[\s*\]|\'\'|null|undefined))' src/
Prevention
- Remember the collection constructors accept nullish and iterable input; never add an empty-literal guard.
- Use `?? fallback` only with non-empty fallback iterables when defaults are truly needed.
- Let `oxlint --fix-suggestions` clean these up mechanically in CI.
When it happens
Trigger: `new Set([])`, `new Set('')`, `new Map(null)`, `new Set(undefined)`, `new WeakSet([])` with exactly one argument (extra arguments disable the check), plus fallback forms `new Set(foo ?? [])`, `new Map(foo ?? '')`, `new Set(foo ?? null)`, `new Set(foo ?? bar ?? [])`, and parenthesized variants like `new Set((([])))`. Not fired for `new Set(foo || [])`, non-empty strings, or `new globalThis.Set([])`.
Common situations: Defensive code written against APIs that throw on nullish input, leftovers after refactoring a producer to always return an iterable, and copy-paste of older fallback patterns. Surfaces when enabling the unicorn plugin or the oxlint style category.
Related errors
- `{ctor_name}` accepts an iterable, so it's unnecessary to co
- Prefer `{} {}` over `{} {}` to check {}.
- Invalid escape sequence in template literal.
- No spaces inside empty pair of braces allowed
- Use uppercase characters for the value of the escape sequenc
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/851a06ba02bab0df.
Report an issue: GitHub.