no-assignment-in-condition
What it does
Section titled “What it does”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.
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”...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;...Configuration
Section titled “Configuration”c: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorcpp: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorcsharp: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorgo: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorjava: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorjs: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorkotlin: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorphp: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorpython: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorruby: lint: rules: no-assignment-in-condition: warn # off (default), warn, errorEnabled 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.
Upstream equivalents
Section titled “Upstream equivalents”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 |