oxc-project/oxc · warning · OxcDiagnostic
Do not call `.add()` immediately after initializing a Set.
Error message
Do not call `.add()` immediately after initializing a Set.
What it means
Diagnostic from the oxlint rule `unicorn/no-immediate-mutation` (Set arm, category: pedantic). `const set = new Set([1, 2]); set.add(3);` builds the Set in two statements; the element can be passed in the constructor's initializer array instead. Fires for the global `Set` and `WeakSet` when `add()` with exactly one non-spread argument (not referencing the set itself) is the statement immediately after the declaration.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs:39
.with_label(span)
}
fn object_assign_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not call `Object.assign()` immediately after initializing an object.")
.with_help("Move the properties from `Object.assign()` into the object initializer.")
.with_label(span)
}
fn object_property_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Do not assign a property immediately after initializing an object literal.",
)
.with_help("Move the property into the object initializer.")
.with_label(span)
}
fn set_add_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not call `.add()` immediately after initializing a Set.")
.with_help("Add the element to the Set initializer array.")
.with_label(span)
}
fn map_set_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not call `.set()` immediately after initializing a Map.")
.with_help("Add the entry to the Map initializer array.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoImmediateMutation;
declare_oxc_lint!(
/// ### What it does
///
/// Disallows mutating a variable immediately after initialization.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Fold the element into the initializer: `const seen = new Set([url]);`
- For many elements spread an iterable: `new Set([...urls, extra])`
- If membership depends on runtime state not expressible in the initializer, keep `add()` and suppress inline
Example fix
// before const seen = new Set(); seen.add(url); // after const seen = new Set([url]);
Defensive patterns
Strategy: validation
Prevention
- Pass initial elements to the Set constructor as an array
- Use `new Set(iterable)` when seeding from another collection
- Keep membership-dependent logic out of initializer position, then suppress deliberately
When it happens
Trigger: `const seen = new Set(); seen.add(url);` or `const weak = new WeakSet([a]); weak.add(b);` — declaration via `new Set`/`new WeakSet` (any arguments, including none) directly followed by `<sameVar>.add(x)`. `add(...spread)`, zero or multiple args, and `set.add(set.size)` pass.
Common situations: Dedupe/visited sets built incrementally; the `const s = new Set(); s.add(x)` idiom carried over from older code; WeakSets used as privacy markers.
Related errors
- Do not call `.set()` immediately after initializing a Map.
- The {expr_type} is useless
- Use `Set#size` instead of converting a `Set` to an array and
- `VirtualFree` failed during cleanup: {err}
- Tried to get an allocator from an empty `FixedSizeAllocatorP
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/2f0b3668cd791530.
Report an issue: GitHub.