Skip to content

Commit

Permalink
[Fix rubocop#8572] Fix a false positive for Style/RedundantParentheses
Browse files Browse the repository at this point in the history
Fixes rubocop#8572.

This PR fixes a false positive for `Style/RedundantParentheses`
when like method argument parentheses.
  • Loading branch information
koic committed Sep 3, 2020
1 parent 199294d commit bf6f7e4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#8627](https://github.com/rubocop-hq/rubocop/issues/8627): Fix a false positive for `Lint/DuplicateRequire` when same feature argument but different require method. ([@koic][])
* [#8572](https://github.com/rubocop-hq/rubocop/issues/8572): Fix a false positive for `Style/RedundantParentheses` when parentheses are used like method argument parentheses. ([@koic][])

## 0.90.0 (2020-09-01)

Expand Down
15 changes: 12 additions & 3 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -26,9 +26,7 @@ class RedundantParentheses < Base
'^^(block (send _ _ equal?(%0) ...) ...)'

def on_begin(node)
return if !parentheses?(node) || parens_allowed?(node)
return if node.parent && (node.parent.while_post_type? ||
node.parent.until_post_type?)
return if !parentheses?(node) || parens_allowed?(node) || ignore_syntax?(node)

check(node)
end
Expand All @@ -42,6 +40,13 @@ def parens_allowed?(node)
allowed_expression?(node)
end

def ignore_syntax?(node)
return false unless (parent = node.parent)

parent.while_post_type? || parent.until_post_type? ||
like_method_argument_parentheses?(parent)
end

def allowed_expression?(node)
allowed_ancestor?(node) ||
allowed_method_call?(node) ||
Expand All @@ -68,6 +73,10 @@ def allowed_multiple_expression?(node)
!ancestor.begin_type? && !ancestor.def_type? && !ancestor.block_type?
end

def like_method_argument_parentheses?(node)
node.send_type? && node.arguments.size == 1 && !node.arithmetic_operation?
end

def empty_parentheses?(node)
# Don't flag `()`
node.children.empty?
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -1563,6 +1563,24 @@ def self.some_method(foo, bar: 1)
RUBY
end

it 'corrects Lint/ParenthesesAsGroupedExpression and offenses and ' \
'accepts Style/RedundantParentheses' do
create_file('example.rb', <<~RUBY)
do_something (argument)
RUBY
expect(
cli.run(
[
'--auto-correct',
'--only', 'Lint/ParenthesesAsGroupedExpression,Style/RedundantParentheses'
]
)
).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
do_something(argument)
RUBY
end

it 'corrects TrailingCommaIn(Array|Hash)Literal and ' \
'Multiline(Array|Hash)BraceLayout offenses' do
create_file('.rubocop.yml', <<~YAML)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -388,6 +388,12 @@ def x
end
end

context 'when parentheses are used like method argument parentheses' do
it 'accepts parens around the arg' do
expect_no_offenses('method (arg)')
end
end

it 'accepts parentheses around the error passed to rescue' do
expect_no_offenses(<<~RUBY)
begin
Expand Down

0 comments on commit bf6f7e4

Please sign in to comment.