Skip to content

no-assignment-in-condition

Reports an assignment used as the condition of an if, a ternary or a loop (if (a = b), while (x = next())) when it is not wrapped in parentheses of its own (if ((a = b)) is accepted). Python’s := is left alone. Off by default.

An assignment in a condition is nearly always a typing error for ==: the test then stores a value and branches on it, and the comparison the author meant never happens. A reader cannot tell the two intentions apart.

To fix, write == if a comparison was meant, or move the assignment out of the condition (or add the extra parentheses) if it was meant.

Reported
...
if (a = b) {} if ((a = b)) {} while (a = b) {} do {} while (a = b); for (; a = b; ) {} for (; (a = b); ) {}
var x = (a = b) ? 1 : 2, y = a = b ? 1 : 2;
...
c:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
cpp:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
csharp:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
go:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
java:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
js:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
kotlin:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
php:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
python:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error
ruby:
lint:
rules:
no-assignment-in-condition: warn # off (default), warn, error

Enabled by the presets: js/eslint-recommended, js/biome-recommended, js/oxlint, ruby/rubocop.

Suppress one line with sudsy-disable-next-line no-assignment-in-condition in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint no-cond-assign partial only the default except-parens mode
rubocop Lint/AssignmentInCondition partial a && b = c is the Ruby rule no-assignment-in-logical-condition; over several lines is reported at the first line of the condition, RuboCop reports the assignment line