forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#5388 This cop checks that `unless` is not used with multiple conditions. In general, using multiple conditions with `unless` reduces readability.
- Loading branch information
Showing
7 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks that `unless` is not used with multiple conditions. | ||
# In general, using multiple conditions with `unless` reduces readability. | ||
# | ||
# @example | ||
# # bad | ||
# unless foo && bar | ||
# something | ||
# end | ||
# | ||
# # bad | ||
# unless foo || bar | ||
# something | ||
# end | ||
# | ||
# # good | ||
# if !foo || !bar | ||
# something | ||
# end | ||
# | ||
# # good | ||
# if !foo && !bar | ||
# something | ||
# end | ||
class UnlessMultipleConditions < Cop | ||
MSG = 'Avoid using `unless` with multiple conditions.'.freeze | ||
|
||
def on_if(node) | ||
return unless node.unless? | ||
|
||
add_offense(node.condition) if node.condition.and_type? || | ||
node.condition.or_type? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::UnlessMultipleConditions do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense when using `unless`' \ | ||
'with multiple `and` conditions' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
unless foo && bar | ||
^^^^^^^^^^ Avoid using `unless` with multiple conditions. | ||
something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `unless` with multiple `or` conditions' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
unless foo || bar | ||
^^^^^^^^^^ Avoid using `unless` with multiple conditions. | ||
something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `if`' \ | ||
'with multiple `and` conditions' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
if !foo && !bar | ||
something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `if`' \ | ||
'with multiple `or` conditions' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
if !foo || !bar | ||
something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `unless` with single condition' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
unless foo | ||
something | ||
end | ||
RUBY | ||
end | ||
end |