Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Lint/ParenthesesAsGroupedExpression #7909

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,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

Expand Down
19 changes: 11 additions & 8 deletions lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
Expand Up @@ -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

Expand All @@ -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?
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions manual/cops_lint.md
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb
Expand Up @@ -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

Expand Down