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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives for Lint/ParenthesesAsGroupedExpression #8039

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
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