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
- Split into two statements: assign, then `return a;`.
- If a comparison was intended, use `===`/`==` explicitly.
- 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
- Never fold assignment into `return`; write the assignment, then return the variable.
- When a comparison is intended, type `===` deliberately and enable eqeqeq to reinforce it.
- Use `"except": ["+="]` only if accumulate-and-return is an accepted team pattern.
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
- Unexpected use of comma operator
- Empty array binding pattern
- Empty object binding pattern
- `debugger` statement is not allowed
- Variables should not be deleted
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/614af967ffabb57e.
Report an issue: GitHub.