Skip to main content

no-return-assign

Assignment in return statements can be confusing - it’s often unclear whether the developer meant to return the assignment or use a comparison operator.
Rule Type: Suggestion
Fixable: No

Why This Rule Exists

Code like this is ambiguous:
Did the developer mean:
  • Return the result of bar + 2 (and assign it to foo)?
  • Use == for comparison instead of =?

Rule Details

This rule disallows assignments in return statements.

Options

"except-parens" (default)

Disallow assignments unless enclosed in parentheses. Incorrect:
Correct:

"always"

Disallow all assignments in return statements, even with parentheses. Incorrect:
Correct:

Examples

Common Mistake: = vs ==

The most common bug is using = when you meant == or ===.

Intentional Assignment

Common Patterns

Caching Pattern

Conditional Assignment

Chained Assignment

Configuration Examples

When Not to Use It

Disable this rule if you:
  • Commonly use assignment in return statements
  • Have a team convention that accepts this pattern
  • Use parentheses to make intent clear
However, for most teams, this rule helps prevent bugs and improves clarity.

Best Practices

Separate Concerns

Separate assignment from return for better readability.

Use Descriptive Variables

Consider Immutability