Skip to content

Commit

Permalink
Fix value omission false positive in Style/MethodCallWithArgsParentheses
Browse files Browse the repository at this point in the history
Omitting the parentheses in cases like the example below is ambigious,
we don't need to enforce their omission in the `EnforcedStyle: omit_parentheses`

```ruby
var =
  unless object.action(value:, other:)
    condition || other_condition
  end
```

If the parentheses in the condition above are omitted, the semantic of
the code is changed:

```ruby
var =
  unless object.action value:, other:
    condition || other_condition
  end

# is interpreted as:

var =
  unless object.action value:, other: condition || other_condition
    # empty branch
  end
```

We should allow parentheses in value omissions in conditional nodes.
  • Loading branch information
gsamokovarov committed Mar 17, 2023
1 parent 43209b3 commit 07b18b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/fix_value_omission_false_positive_in.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11709](https://github.com/rubocop/rubocop/pull/11709): Fix value omission false positive in `Style/MethodCallWithArgsParentheses`. ([@gsamokovarov][])
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,13 @@ def require_parentheses_for_hash_value_omission?(node)
return false unless (last_argument = node.last_argument)
return false if !last_argument.hash_type? || !last_argument.pairs.last&.value_omission?

modifier_form?(node) || exist_next_line_expression?(node)
end

def modifier_form?(node)
node.parent.respond_to?(:modifier_form?) && node.parent.modifier_form?
node.parent&.conditional? || !last_expression?(node)
end

# Require hash value omission be enclosed in parentheses to prevent the following issue:
# https://bugs.ruby-lang.org/issues/18396.
def exist_next_line_expression?(node)
node.parent&.assignment? ? node.parent.right_sibling : node.right_sibling
def last_expression?(node)
!(node.parent&.assignment? ? node.parent.right_sibling : node.right_sibling)
end

def syntax_like_method_call?(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,15 @@ def method_missing(name, ...)
foo arg
RUBY
end

it 'does not register an offense in conditionals' do
expect_no_offenses(<<~RUBY)
var =
unless object.action(value:, other:)
condition || other_condition
end
RUBY
end
end

context 'anonymous rest arguments in 3.2', :ruby32 do
Expand Down

0 comments on commit 07b18b5

Please sign in to comment.