oxc-project/oxc · warning · OxcDiagnostic

Returned expression contains an assignment.

Error message

Returned expression contains an assignment.

What it means

oxlint's port of ESLint `no-return-assign`. It flags `return` statements whose expression is an assignment, because `return a = b` looks like a comparison/typo and returns the assigned value as a side effect. Mode `always` (default) flags every assignment operator; mode `except` allows the listed operators (e.g. `+=`) and flags the rest.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_return_assign.rs:16

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;

use crate::{
    AstNode,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
};

fn no_return_assign_diagnostic(span: Span, help: &'static str) -> OxcDiagnostic {
    OxcDiagnostic::warn("Returned expression contains an assignment.")
        .with_label(span)
        .with_help(help)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct NoReturnAssign(NoReturnAssignMode);

#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum NoReturnAssignMode {
    /// Disallow all assignments in return statements.
    Always,
    /// Allow assignments in return statements only if they are enclosed in parentheses.
    /// This is the default mode.
    #[default]
    ExceptParens,
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Split into two statements: assign, then `return a;`.
  2. If a comparison was intended, use `===`/`==` explicitly.
  3. For accumulate-and-return patterns, switch the rule to `"except": ["+=", "-="]` in .oxlintrc.

Example fix

// before
function f(a, b) {
  return a = b;
}

// after
function f(a, b) {
  a = b;
  return a;
}
Defensive patterns

Strategy: validation

Validate before calling

// Flag `return <expr> =` / `return <expr> +=` shapes
function hasReturnAssign(src) {
  return /return\s+[^;\n]+(?::?=|\+=|-=|\*=|\/=|&&=|\|\||\?\?=)[^=]/.test(src);
}

Prevention

When it happens

Trigger: `function f(a, b) { return a = b; }` in always mode; `return total += x;` is allowed only under `"except": ["+="]`; arrow-concise bodies with assignment produce the same diagnostic.

Common situations: Condensing initialization and return into one line; porting old C-style code; typos where `==`/`===` was intended (`return a = b` vs `return a == b`).

Related errors


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