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

  1. Expand the shorthand: `x += y` becomes `x = x + y`.
  2. If shorthand is preferred (the common style), configure `["error", "always"]` — the default.
  3. Use an inline disable comment for lines where the compound form is deliberate (e.g. bitwise flag updates).
  4. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/48cae1340dcf39fd. Report an issue: GitHub.