oxc-project/oxc · warning · OxcDiagnostic
Unexpected operator assignment ({operator}) shorthand.
Error message
Unexpected operator assignment ({operator}) shorthand. What it means
Diagnostic from the oxlint `operator-assignment` rule in `never` mode. It flags compound assignment operators (`+=`, `-=`, `*=`, `<<=`, etc.) and wants them expanded to regular `=` assignments, e.g. `x += y` should be `x = x + y` (operator_assignment.rs:33-38). When the linter cannot auto-fix, a note explains the manual replacement.
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
- Expand the shorthand: `x += y` becomes `x = x + y`.
- If shorthand is preferred (the common style), configure `["error", "always"]` — the default.
- Use an inline disable comment for lines where the compound form is deliberate (e.g. bitwise flag updates).
- Run `oxlint --fix` for the auto-fixable cases (simple `identifier op= expr`).
Example fix
// before x += y; // after x = x + y;
Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json — "always" is the common default
{ "rules": { "operator-assignment": ["warn", "always"] } } Prevention
- Use compound operators (`+=`) by default; "never" mode is rare.
- If "never" is house style, expand shorthands as you write them or run `oxlint --fix`.
- Double-check the mode when copying config from another team.
When it happens
Trigger: Configure `"operator-assignment": ["error", "never"]` and lint any compound assignment such as `x *= 2` or `flags |= MASK`. The `{operator}` placeholder is the compound operator found.
Common situations: Configs ported from teams that find compound operators harder to read in diffs; search-and-replace style code audits that match on `x = x + ...`; accidentally leaving "never" after copying an unrelated config block.
Related errors
- Assignment (=) can be replaced with operator assignment ({op
- 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/48cae1340dcf39fd.
Report an issue: GitHub.