{"record":{"id":"b114d5411b4e3e08","repo":"oxc-project/oxc","slug":"assignment-can-be-replaced-with-operator-assig","errorCode":null,"errorMessage":"Assignment (=) can be replaced with operator assignment ({operator}).","messagePattern":"Assignment \\(=\\) can be replaced with operator assignment \\((.+?)\\)\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/operator_assignment.rs","lineNumber":33,"sourceCode":"use crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::{AlwaysNever, is_same_member_expression},\n};\n\nfn operator_assignment_diagnostic(\n    mode: &AlwaysNever,\n    span: Span,\n    operator: &str,\n    can_fix: bool,\n) -> OxcDiagnostic {\n    let msg = if &AlwaysNever::Never == mode {\n        format!(\"Unexpected operator assignment ({operator}) shorthand.\")\n    } else {\n        format!(\"Assignment (=) can be replaced with operator assignment ({operator}).\")\n    };\n    let mut diagnostic = OxcDiagnostic::warn(msg).with_label(span);\n\n    if !can_fix {\n        diagnostic = diagnostic.with_note(if &AlwaysNever::Never == mode {\n            format!(\"Replace '{operator}' with a regular '=' assignment.\")\n        } else {\n            format!(\"Use '{operator}' shorthand instead of '='.\")\n        });\n    }\n\n    diagnostic\n}\n\n#[derive(Debug, Default, Clone, Serialize, Deserialize)]\npub struct OperatorAssignment(AlwaysNever);\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/operator_assignment.rs#L15-L51","documentation":"Diagnostic from the oxlint `operator-assignment` rule in `always` mode (the default). It flags a plain assignment whose right-hand side repeats the target, e.g. `x = x + y`, and suggests the compound operator `x += y` (operator_assignment.rs:33-38). When the expression is not safely transformable (can_fix=false), the diagnostic carries a note telling you to apply the shorthand manually.","triggerScenarios":"Enable `\"operator-assignment\"` (default \"always\") and write `x = x + 1`, `total = total - fee`, or `str = str + suffix`. The `{operator}` placeholder names the matching compound operator (+ maps to +=).","commonSituations":"Verbose code produced by code generators or beginner-style tutorials; refactors that inline a variable into `x = x + ...`; cases like `x = x + y + z` where the fixer declines (operand grouping changes semantics) and only a note is emitted.","solutions":["Replace with the compound operator: `x = x + y` becomes `x += y` (run `oxlint --fix` for the safe cases).","For non-fixable shapes such as `x = 0 * x + y` or `x = (x + y) + 1`... restructure so the target is the entire RHS start, or rewrite manually.","Set mode to \"never\" only if your team explicitly bans compound operators.","Inline-disable when readability genuinely suffers (e.g. `width = width / 2 + 1`)."],"exampleFix":"// before\nx = x + y;\n\n// after\nx += y;","handlingStrategy":"validation","validationCode":"// keep default mode; let the fixer handle it\n// package.json (lint-staged): \"*.js\": \"oxlint --fix --fix-dangerously=false\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write `x += y` directly instead of `x = x + y`.","Run `oxlint --fix` — simple identifier-target cases are auto-fixed.","For non-fixable shapes, restructure so the assignee is exactly the start of the RHS."],"tags":["oxlint","eslint","style","operators","assignment"],"backgroundTag":"eslint-operator-assignment","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"}