diff --git a/CHANGELOG.md b/CHANGELOG.md index 07a5b4ad50e..ec873f2e654 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) ### Changes diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index 3a040adef50..945e19a9058 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -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 @@ -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) || @@ -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? diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index 831114a549b..257b26cdd45 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -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) diff --git a/spec/rubocop/cop/style/redundant_parentheses_spec.rb b/spec/rubocop/cop/style/redundant_parentheses_spec.rb index b11cc6c4160..0eeb6cbce6b 100644 --- a/spec/rubocop/cop/style/redundant_parentheses_spec.rb +++ b/spec/rubocop/cop/style/redundant_parentheses_spec.rb @@ -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