#8879·rubocop

New cop that suggests replacing complex `unless` with `if`

Author: fatkodimaCreated Oct 9, 2020Updated Sep 2, 2026
Labelsfeature request

I was thinking about this before, but after this thread - https://twitter.com/nateberkopec/status/1314235606705340418, I finally decided to write here to start a discussion. Turns out there are a lot of people struggling to parse non-trivial unless statements, and i am amongst them.

Imo, while something like do_something unless cond is expressive and easy to parse and understand (and even something like do_something unless cond1 && cond2), when conditions become more complex (having || or !) it is hard to do so.

So, I suggest adding a cop (disabled by default), that will suggest rewriting complex unless with if.

ruby
# bad
do_something unless foo || bar
do_something unless foo || !bar
do_something unless !foo || bar || !quux
# ...

# good
do_something unless foo

do_something if !foo && !bar
do_something if !foo && bar
do_something if foo && !bar && quux

# neutral?
do_something unless foo && bar