Skip to content

Commit

Permalink
[Fix rubocop#12529] Fix false positives for `Style/ParenthesesAroundC…
Browse files Browse the repository at this point in the history
…ondition`

Fixes rubocop#12529.

This PR fixes false positives for `Style/ParenthesesAroundCondition` when
using a method call with `do`...`end` block as a condition in a loop.
  • Loading branch information
koic committed Dec 12, 2023
1 parent 25f2bb6 commit 0475db2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12529](https://github.com/rubocop/rubocop/issues/12529): Fix false positives for `Style/ParenthesesAroundCondition`. ([@koic][])
8 changes: 8 additions & 0 deletions lib/rubocop/cop/style/parentheses_around_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def process_control_op(node)
cond = node.condition

control_op_condition(cond) do |first_child, rest_children|
return if require_parentheses?(node, first_child)
return if semicolon_separated_expressions?(first_child, rest_children)
return if modifier_op?(first_child)
return if parens_allowed?(cond)
Expand All @@ -92,6 +93,13 @@ def process_control_op(node)
end
end

def require_parentheses?(node, condition_body)
return false if !node.while_type? && !node.until_type?
return false if !condition_body.block_type? && !condition_body.numblock_type?

condition_body.send_node.block_literal? && condition_body.keywords?
end

def semicolon_separated_expressions?(first_exp, rest_exps)
return false unless (second_exp = rest_exps.first)

Expand Down
94 changes: 94 additions & 0 deletions spec/rubocop/cop/style/parentheses_around_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,100 @@
RUBY
end

it 'does not register an offense when using a method call with `do`...`end` block as a condition in `while` loop' do
expect_no_offenses(<<~RUBY)
while (foo do
end)
end
RUBY
end

it 'does not register an offense when using a method call with `do`...`end` block as a condition in `until` loop' do
expect_no_offenses(<<~RUBY)
until (foo do
end)
end
RUBY
end

it 'does not register an offense when using a method call with `do`...`end` numbered block as a condition in `while` loop' do
expect_no_offenses(<<~RUBY)
while (foo do
_1
end)
end
RUBY
end

it 'does not register an offense when using a method call with `do`...`end` numbered block as a condition in `until` loop' do
expect_no_offenses(<<~RUBY)
until (foo do
_1
end)
end
RUBY
end

it 'registers an offense when using method call with `{`...`}` block as a `while` condition' do
expect_offense(<<~RUBY)
while (foo {
^^^^^^ Don't use parentheses around the condition of a `while`.
})
end
RUBY

expect_correction(<<~RUBY)
while foo {
}
end
RUBY
end

it 'registers an offense when using method call with `{`...`}` block as a `until` condition' do
expect_offense(<<~RUBY)
until (foo {
^^^^^^ Don't use parentheses around the condition of an `until`.
})
end
RUBY

expect_correction(<<~RUBY)
until foo {
}
end
RUBY
end

it 'registers an offense when using method call with `do`...`end` block as a `if` condition' do
expect_offense(<<~RUBY)
if (foo do
^^^^^^^ Don't use parentheses around the condition of an `if`.
end)
end
RUBY

expect_correction(<<~RUBY)
if foo do
end
end
RUBY
end

it 'registers an offense when using method call with `{`...`}` block as a `if` condition' do
expect_offense(<<~RUBY)
if (foo {
^^^^^^ Don't use parentheses around the condition of an `if`.
})
end
RUBY

expect_correction(<<~RUBY)
if foo {
}
end
RUBY
end

it 'does not register an offense when parentheses in multiple expressions separated by semicolon' do
expect_no_offenses(<<~RUBY)
if (foo; bar)
Expand Down

0 comments on commit 0475db2

Please sign in to comment.