Skip to content

Commit

Permalink
[Fix rubocop#12614] Fix false positiveis for `Style/RedundantParenthe…
Browse files Browse the repository at this point in the history
…ses`

Fixes rubocop#12614.

This PR fixes false positiveis for `Style/RedundantParentheses`
when parentheses in control flow keyword with multiline style argument.
  • Loading branch information
koic committed Jan 16, 2024
1 parent ce906c6 commit b9c4f58
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12614](https://github.com/rubocop/rubocop/issues/12614): Fix false positiveis for `Style/RedundantParentheses` when parentheses in control flow keyword with multiline style argument. ([@koic][])
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/redundant_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def ignore_syntax?(node)
return false unless (parent = node.parent)

parent.while_post_type? || parent.until_post_type? || parent.match_with_lvasgn_type? ||
like_method_argument_parentheses?(parent)
like_method_argument_parentheses?(parent) || multiline_control_flow_statements?(node)
end

def allowed_expression?(node)
Expand Down Expand Up @@ -104,6 +104,13 @@ def like_method_argument_parentheses?(node)
!node.arithmetic_operation? && node.first_argument.begin_type?
end

def multiline_control_flow_statements?(node)
return false unless (parent = node.parent)
return false if parent.single_line?

parent.return_type? || parent.next_type? || parent.break_type?
end

def empty_parentheses?(node)
# Don't flag `()`
node.children.empty?
Expand Down
57 changes: 57 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,63 @@ def foo
RUBY
end

it 'accepts parentheses in `return` with multiline style argument' do
expect_no_offenses(<<~RUBY)
return (
42
)
RUBY
end

it 'registers an offense when parentheses in `return` with single style argument' do
expect_offense(<<~RUBY)
return (42)
^^^^ Don't use parentheses around a literal.
RUBY

expect_correction(<<~RUBY)
return 42
RUBY
end

it 'accepts parentheses in `next` with multiline style argument' do
expect_no_offenses(<<~RUBY)
next (
42
)
RUBY
end

it 'registers an offense when parentheses in `next` with single style argument' do
expect_offense(<<~RUBY)
next (42)
^^^^ Don't use parentheses around a literal.
RUBY

expect_correction(<<~RUBY)
next 42
RUBY
end

it 'accepts parentheses in `break` with multiline style argument' do
expect_no_offenses(<<~RUBY)
break (
42
)
RUBY
end

it 'registers an offense when parentheses in `break` with single style argument' do
expect_offense(<<~RUBY)
break (42)
^^^^ Don't use parentheses around a literal.
RUBY

expect_correction(<<~RUBY)
break 42
RUBY
end

it 'registers an offense and corrects when method arguments are unnecessarily parenthesized' do
expect_offense(<<~RUBY)
foo(
Expand Down

0 comments on commit b9c4f58

Please sign in to comment.