Skip to content

Commit

Permalink
Fix false positives for Lint/ParenthesesAsGroupedExpression
Browse files Browse the repository at this point in the history
This PR fixes the following false positive for `Lint/ParenthesesAsGroupedExpression`

when an expression is followed by an operator

```ruby
func (x) || y
```

when an expression is followed by a chain of functions

```ruby
func (x).func.func.func.func.func
```

This PR adds some reproduction test
  • Loading branch information
camille committed Jul 1, 2020
1 parent c56caa9 commit 5421042
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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][])
Expand Down Expand Up @@ -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
11 changes: 8 additions & 3 deletions lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5421042

Please sign in to comment.