Skip to content

Commit

Permalink
[Fix rubocop#10401] Fix a false positive for Style/HashSyntax
Browse files Browse the repository at this point in the history
Fixes rubocop#10401.

This PR fixes a false positive for `Style/HashSyntax`
when local variable hash key and hash value are the same.
  • Loading branch information
koic committed Feb 5, 2022
1 parent 8ae9e0f commit ecdf9f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_style_hash_syntax.md
@@ -0,0 +1 @@
* [#10401](https://github.com/rubocop/rubocop/issues/10401): Fix a false positive for `Style/HashSyntax` when local variable hash key and hash value are the same. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/hash_shorthand_syntax.rb
Expand Up @@ -41,7 +41,7 @@ def enforced_shorthand_syntax
end

def require_hash_value?(hash_key_source, node)
return true if require_hash_value_for_around_hash_literal?(node)
return true if !node.key.sym_type? || require_hash_value_for_around_hash_literal?(node)

hash_value = node.value
return true unless hash_value.send_type? || hash_value.lvar_type?
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Expand Up @@ -959,6 +959,19 @@ def do_something
RUBY
end

it 'does not register an offense when method call hash key and hash value are the same' do
expect_no_offenses(<<~RUBY)
{foo => foo}
RUBY
end

it 'does not register an offense when lvar hash key and hash value are the same' do
expect_no_offenses(<<~RUBY)
foo = 42
{foo => foo}
RUBY
end

it 'does not register an offense when without parentheses call expr follows' do
# Prevent syntax errors shown in the URL: https://bugs.ruby-lang.org/issues/18396
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit ecdf9f1

Please sign in to comment.