diff --git a/changelog/fix_a_false_positive_for_lint_parentheses_as_grouped_expression.md b/changelog/fix_a_false_positive_for_lint_parentheses_as_grouped_expression.md new file mode 100644 index 00000000000..f4a204a7d68 --- /dev/null +++ b/changelog/fix_a_false_positive_for_lint_parentheses_as_grouped_expression.md @@ -0,0 +1 @@ +* [#10719](https://github.com/rubocop/rubocop/issues/10719): Fix a false positive for `Lint/ParenthesesAsGroupedExpression` when using safe navigation operator. ([@koic][]) diff --git a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb index b045e8665fc..e155c2914f5 100644 --- a/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +++ b/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb @@ -55,7 +55,7 @@ def first_argument_starts_with_left_parenthesis?(node) def chained_calls?(node) first_argument = node.first_argument - first_argument.send_type? && (node.children.last&.children&.count || 0) > 1 + first_argument.call_type? && (node.children.last&.children&.count || 0) > 1 end def ternary_expression?(node) 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 1da8a600dc6..723a84b452a 100644 --- a/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +++ b/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb @@ -36,6 +36,12 @@ RUBY end + it 'does not register an offense for expression followed by chained expression with safe navigation operator' 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