diff --git a/CHANGELOG.md b/CHANGELOG.md index dfd8931641d..118d5226254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Bug fixes +* [#8039](https://github.com/rubocop-hq/rubocop/pull/8039): Fix false positives for `Lint/ParenthesesAsGroupedExpression` in when using operators or chain functions. ([@CamilleDrapier][]) * [#8196](https://github.com/rubocop-hq/rubocop/issues/8196): Fix a false positive for `Style/RedundantFetchBlock` when using with `Rails.cache`. ([@fatkodima][]) * [#8195](https://github.com/rubocop-hq/rubocop/issues/8195): Fix an error for `Style/RedundantFetchBlock` when using `#fetch` with empty block. ([@koic][]) * [#8193](https://github.com/rubocop-hq/rubocop/issues/8193): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using `[\b]`. ([@owst][]) @@ -4633,3 +4634,4 @@ [@mauro-oto]: https://github.com/mauro-oto [@fatkodima]: https://github.com/fatkodima [@karlwithak]: https://github.com/karlwithak +[@CamilleDrapier]: https://github.com/CamilleDrapier diff --git a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb index c6c82cb80a9..c3efd8f1b1c 100644 --- a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +++ b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb @@ -48,17 +48,22 @@ def valid_context?(node) return true end - node.operator_method? || node.setter_method? || grouped_parentheses?(node) + node.operator_method? || node.setter_method? || chained_calls?(node) || + operator_keyword?(node) end def first_argument_starts_with_left_parenthesis?(node) node.first_argument.source.start_with?('(') end - def grouped_parentheses?(node) + def chained_calls?(node) first_argument = node.first_argument + first_argument.send_type? && (node.children.last&.children&.count || 0) > 1 + end - first_argument.send_type? && first_argument.receiver&.begin_type? + def operator_keyword?(node) + first_argument = node.first_argument + first_argument.operator_keyword? end def spaces_before_left_parenthesis(node) diff --git a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb index e21f7ca0ac5..2af707467be 100644 --- a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +++ b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb @@ -27,6 +27,18 @@ RUBY end + it 'does not register an offense for expression followed by an operator' do + expect_no_offenses(<<~RUBY) + func (x) || y + RUBY + end + + it 'does not register an offense for expression followed by chained expression' do + expect_no_offenses(<<~RUBY) + func (x).func.func.func.func.func + RUBY + end + it 'does not register an offense for math expression' do expect_no_offenses(<<~RUBY) puts (2 + 3) * 4