oxc-project/oxc · warning · OxcDiagnostic
Assignment (=) can be replaced with operator assignment ({op
Error message
Assignment (=) can be replaced with operator assignment ({operator}). What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/eslint/operator_assignment.rs:33
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
utils::{AlwaysNever, is_same_member_expression},
};
fn operator_assignment_diagnostic(
mode: &AlwaysNever,
span: Span,
operator: &str,
can_fix: bool,
) -> OxcDiagnostic {
let msg = if &AlwaysNever::Never == mode {
format!("Unexpected operator assignment ({operator}) shorthand.")
} else {
format!("Assignment (=) can be replaced with operator assignment ({operator}).")
};
let mut diagnostic = OxcDiagnostic::warn(msg).with_label(span);
if !can_fix {
diagnostic = diagnostic.with_note(if &AlwaysNever::Never == mode {
format!("Replace '{operator}' with a regular '=' assignment.")
} else {
format!("Use '{operator}' shorthand instead of '='.")
});
}
diagnostic
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct OperatorAssignment(AlwaysNever);
declare_oxc_lint!(
/// ### What it does
///View on GitHub (pinned to e1e7af627c)
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`).
Example fix
// before x = x + y; // after x += y;
Defensive patterns
Strategy: validation
Validate before calling
// keep default mode; let the fixer handle it // package.json (lint-staged): "*.js": "oxlint --fix --fix-dangerously=false"
Prevention
- 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.
When it happens
Trigger: 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 +=).
Common situations: 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.
Related errors
- Unexpected operator assignment ({operator}) shorthand.
- Require or disallow "Yoda" conditions
- Expected method{method_name_str} to have this.
- {prefix} {{ after '{keyword}'{condition_if_needed}.
- Enforce default clauses in switch statements to be last
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/b114d5411b4e3e08.
Report an issue: GitHub.