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

  1. Remove the useless argument or fallback: `new Set(foo ?? [])` -> `new Set(foo)`, `new Set([])` -> `new Set()`.
  2. Apply the rule's suggestion fix via `oxlint --fix-suggestions` or your editor quick fix.
  3. If you need default entries, keep a non-empty fallback: `new Set(foo ?? [defaultItem])`.
  4. 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

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


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