Skip to content

Commit

Permalink
Merge pull request #10497 from koic/fix_a_false_positive_for_style_ha…
Browse files Browse the repository at this point in the history
…sh_syntax

[Fix #10494] Fix a false positive for `Style/HashSyntax`
  • Loading branch information
koic committed Apr 2, 2022
2 parents fc9018f + e7dc7d3 commit 7a192de
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_style_hash_syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10494](https://github.com/rubocop/rubocop/issues/10494): Fix a false positive for `Style/HashSyntax` when `return` with one line `if` condition follows (without parentheses). ([@koic][])
3 changes: 1 addition & 2 deletions lib/rubocop/cop/mixin/hash_shorthand_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def use_element_of_hash_literal_as_receiver?(ancestor, parent)

def use_modifier_form_without_parenthesized_method_call?(ancestor)
return false if ancestor.respond_to?(:parenthesized?) && ancestor.parenthesized?
return false unless (parent = ancestor.parent)

parent.respond_to?(:modifier_form?) && parent.modifier_form?
ancestor.ancestors.any? { |node| node.respond_to?(:modifier_form?) && node.modifier_form? }
end

def without_parentheses_call_expr_follows?(ancestor)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,12 @@ def do_something
RUBY
end

it 'does not register an offense when `return` with one line `if` condition follows (without parentheses)' do
expect_no_offenses(<<~RUBY)
return foo value: value if bar
RUBY
end

it 'registers an offense when one line `until` condition follows (with parentheses)' do
expect_offense(<<~RUBY)
foo(value: value) until bar
Expand Down

0 comments on commit 7a192de

Please sign in to comment.