Skip to content

no-assignment-in-logical-condition

Rubysuspiciousno fixdefault: warnruby/rubocop

Reports a condition that is a logical operation (&&, ||) with an assignment as the right operand not in parentheses (if a && b = c). An assignment as the whole condition is no-assignment-in-condition.

In a && b = c the assignment is easy to read as a comparison (==), and the value of the whole condition is the assigned value, so the code branches on the wrong thing.

To fix, use == if a comparison was meant, or put the assignment in parentheses if it is on purpose.

Reported
...
when 1 then y
...
end
if a && b = c
d
end
begin
...
ruby:
lint:
rules:
no-assignment-in-logical-condition: warn # off, warn (default), error

Enabled by the presets: ruby/rubocop.

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