{"record":{"id":"2f0b3668cd791530","repo":"oxc-project/oxc","slug":"do-not-call-add-immediately-after-initializin","errorCode":null,"errorMessage":"Do not call `.add()` immediately after initializing a Set.","messagePattern":"Do not call `\\.add\\(\\)` immediately after initializing a Set\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs","lineNumber":39,"sourceCode":"    .with_label(span)\n}\n\nfn object_assign_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not call `Object.assign()` immediately after initializing an object.\")\n        .with_help(\"Move the properties from `Object.assign()` into the object initializer.\")\n        .with_label(span)\n}\n\nfn object_property_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Do not assign a property immediately after initializing an object literal.\",\n    )\n    .with_help(\"Move the property into the object initializer.\")\n    .with_label(span)\n}\n\nfn set_add_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not call `.add()` immediately after initializing a Set.\")\n        .with_help(\"Add the element to the Set initializer array.\")\n        .with_label(span)\n}\n\nfn map_set_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not call `.set()` immediately after initializing a Map.\")\n        .with_help(\"Add the entry to the Map initializer array.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoImmediateMutation;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows mutating a variable immediately after initialization.\n    ///","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs#L21-L57","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Dedupe/visited sets built incrementally; the `const s = new Set(); s.add(x)` idiom carried over from older code; WeakSets used as privacy markers.","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"],"exampleFix":"// before\nconst seen = new Set();\nseen.add(url);\n\n// after\nconst seen = new Set([url]);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["set","collections","style","oxlint"],"backgroundTag":"mutate-after-init","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}