diff --git a/changelog/fix_a_false_negative_for_style_redundant_parentheses.md b/changelog/fix_a_false_negative_for_style_redundant_parentheses.md new file mode 100644 index 00000000000..6fc3def954f --- /dev/null +++ b/changelog/fix_a_false_negative_for_style_redundant_parentheses.md @@ -0,0 +1 @@ +* [#10785](https://github.com/rubocop/rubocop/pull/10785): Fix a false negative for `Style/RedundantParentheses` when parens around a receiver of a method call with an argument. ([@koic][]) diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index 6f2ac089b86..e73dd570808 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -81,7 +81,8 @@ def allowed_multiple_expression?(node) end def like_method_argument_parentheses?(node) - node.send_type? && node.arguments.size == 1 && !node.arithmetic_operation? + node.send_type? && node.arguments.one? && + !node.arithmetic_operation? && node.first_argument.begin_type? end def empty_parentheses?(node) diff --git a/spec/rubocop/cop/style/redundant_parentheses_spec.rb b/spec/rubocop/cop/style/redundant_parentheses_spec.rb index be3da5b7408..98606dc489b 100644 --- a/spec/rubocop/cop/style/redundant_parentheses_spec.rb +++ b/spec/rubocop/cop/style/redundant_parentheses_spec.rb @@ -125,6 +125,17 @@ it_behaves_like 'plausible', '+(1.foo.bar)' it_behaves_like 'plausible', '()' + it 'registers an offense for parens around a receiver of a method call with an argument' do + expect_offense(<<~RUBY) + (x).y(z) + ^^^ Don't use parentheses around a method call. + RUBY + + expect_correction(<<~RUBY) + x.y(z) + RUBY + end + it 'registers an offense for parens around an interpolated expression' do expect_offense(<<~RUBY) "\#{(foo)}"