diff --git a/CHANGELOG.md b/CHANGELOG.md index 862bf031c6d..c92e5c411bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * [#7903](https://github.com/rubocop-hq/rubocop/pull/7903): Fix an incorrect autocorrect for `Style/HashTransformKeys` and `Style/HashTransformValues` cops when line break before `to_h` method. ([@diogoosorio][], [@koic][]) * [#7899](https://github.com/rubocop-hq/rubocop/issues/7899): Fix an infinite loop error for `Layout/SpaceAroundOperators` with `Layout/ExtraSpacing` when using `ForceEqualSignAlignment: true`. ([@koic][]) * [#7885](https://github.com/rubocop-hq/rubocop/issues/7885): Fix `Style/IfUnlessModifier` logic when tabs are used for indentation. ([@jonas054][]) +* [#7909](https://github.com/rubocop-hq/rubocop/pull/7909): Fix a false positive for `Lint/ParenthesesAsGroupedExpression` when using an intended grouped parentheses. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb index b23a5dc2f1c..4a21bb7b8d9 100644 --- a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +++ b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb @@ -9,14 +9,12 @@ module Lint # @example # # # bad - # - # puts (x + y) - # - # @example + # do_something (foo) # # # good - # - # puts(x + y) + # do_something(foo) + # do_something (2 + 3) * 4 + # do_something (foo * bar).baz class ParenthesesAsGroupedExpression < Cop include RangeHelp @@ -25,8 +23,7 @@ class ParenthesesAsGroupedExpression < Cop def on_send(node) return unless node.arguments.one? return if node.operator_method? || node.setter_method? - - return unless node.first_argument.source.start_with?('(') + return if grouped_parentheses?(node) space_length = spaces_before_left_parenthesis(node) return unless space_length.positive? @@ -39,6 +36,12 @@ def on_send(node) private + def grouped_parentheses?(node) + first_argument = node.first_argument + + first_argument.send_type? && first_argument.receiver&.begin_type? + end + def spaces_before_left_parenthesis(node) receiver = node.receiver receiver_length = if receiver diff --git a/manual/cops_lint.md b/manual/cops_lint.md index c93d9f750c0..423e8817261 100644 --- a/manual/cops_lint.md +++ b/manual/cops_lint.md @@ -1424,13 +1424,12 @@ parenthesis. ```ruby # bad +do_something (foo) -puts (x + y) -``` -```ruby # good - -puts(x + y) +do_something(foo) +do_something (2 + 3) * 4 +do_something (foo * bar).baz ``` ### References 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 573ae9883b0..b027b4b3605 100644 --- a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +++ b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb @@ -19,10 +19,15 @@ RUBY end - it 'registers an offense for math expression' do - expect_offense(<<~RUBY) + it 'does not register an offense for math expression' do + expect_no_offenses(<<~RUBY) puts (2 + 3) * 4 - ^ `(...)` interpreted as grouped expression. + RUBY + end + + it 'does not register an offense for math expression with `to_i`' do + expect_no_offenses(<<~RUBY) + do_something.eq (foo * bar).to_i RUBY end