diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aa88926e95..6150e7eeb62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#7639](https://github.com/rubocop-hq/rubocop/pull/7639): Fix logical operator edge case in `omit_parentheses` style of `Style/MethodCallWithArgsParentheses`. ([@gsamokovarov][]) + ### Changes * [#7636](https://github.com/rubocop-hq/rubocop/issues/7636): Remove `console` from `Lint/Debugger` to prevent false positives. ([@gsamokovarov][]) diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb index 2e44be54ac4..dc8505662ab 100644 --- a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb @@ -74,10 +74,11 @@ def call_in_literals?(node) end def call_in_logical_operators?(node) - node.parent && - (logical_operator?(node.parent) || - node.parent.send_type? && - node.parent.arguments.any?(&method(:logical_operator?))) + parent = node.parent&.block_type? ? node.parent.parent : node.parent + parent && + (logical_operator?(parent) || + parent.send_type? && + parent.arguments.any?(&method(:logical_operator?))) end def call_in_optional_arguments?(node) diff --git a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb index 82d8ac40ce4..a02234a4197 100644 --- a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb +++ b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb @@ -471,6 +471,11 @@ def seatle_style arg: default(42) it 'accepts parens in calls with logical operators' do expect_no_offenses('foo(a) && bar(b)') expect_no_offenses('foo(a) || bar(b)') + expect_no_offenses(<<~RUBY) + foo(a) || bar(b) do + pass + end + RUBY end it 'accepts parens in calls with args with logical operators' do