rubocop-avoid_unless
A RuboCop extension that bans unless in favor of if with an inverse or negated condition. It provides a single cop, Style/AvoidUnless, with full auto-correction support.
Why
unless can harm readability, especially with compound or negated conditions (unless !done?, unless x && y). This cop enforces a consistent if-only style across your codebase.
Installation
Add the gem to your Gemfile or gemspec:
gem "rubocop-avoid_unless"Then run bundle install.
Setup
With Standard Ruby
If you use Standard Ruby, add the plugin to your .standard.yml:
plugins:
- rubocop-avoid_unlessStandard will load the cop automatically via the lint_roller plugin system. No other configuration is needed.
With RuboCop directly
If you use RuboCop without Standard, there are two options:
Option A - Use the lint_roller plugin system (RuboCop 1.72+). Add to your .rubocop.yml:
plugins:
- rubocop-avoid_unlessOption B - Use the traditional require approach. Add to your .rubocop.yml:
require:
- rubocop-avoid_unlessConfiguration
The cop is enabled by default once loaded. To disable or customize it, add to your .rubocop.yml:
Style/AvoidUnless:
Enabled: falseHow it works
The cop flags every use of unless (both modifier and block forms) and auto-corrects to if by inverting the condition:
# bad
do_something unless items.any?
return unless value.present?
return unless condition
# good (auto-corrected)
do_something if items.none?
return if value.blank?
return if !conditionIt handles:
-
Inverse methods -
any?/none?,present?/blank?,include?/exclude?,even?/odd?, etc. -
Inverse operators -
==/!=,>/<=,=~/!~, etc. -
Double negation removal -
unless !xbecomesif x -
De Morgan's law -
unless a && bbecomesif !a || !b
Requirements
- Ruby >= 3.2
- RuboCop >= 1.72
License
MIT