Skip to content

Commit

Permalink
Fix a false positive for Lint/ParenthesesAsGroupedExpression
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Lint/ParenthesesAsGroupedExpression`
when using an intended grouped parentheses.

```ruby
puts (2 + 3) * 4
```

I noticed this false positive in implementing this cop's auto-correction.
So, I plan to open a PR that implements auto-correction separately from
this PR.
  • Loading branch information
koic committed May 7, 2020
1 parent 21a1b2b commit 5d5cf77
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
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

0 comments on commit 5d5cf77

Please sign in to comment.