Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive for "Style/HashSyntax" when using a variable as a hash key #10401

Closed
mehanoid opened this issue Feb 5, 2022 · 4 comments · Fixed by #10402
Closed

False positive for "Style/HashSyntax" when using a variable as a hash key #10401

mehanoid opened this issue Feb 5, 2022 · 4 comments · Fixed by #10402
Labels

Comments

@mehanoid
Copy link

mehanoid commented Feb 5, 2022

If you use the same variable as both a hash key and its value, then ruboсop wants to omit the value. The proposed autocorrection breaks the semantics of the code.

Expected behavior

No offences for the following code:

# frozen_string_literal: true

var = 1
{ var => var } # resulting in { 1 => 1}

Actual behavior

For /home/meh/Projects/rubocop-test: Default configuration from /home/meh/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/bundler/gems/rubocop-8ae9e0f2081d/config/default.yml
Inspecting 1 file
Scanning /home/meh/Projects/rubocop-test/hash.rb
Loading cache from /home/meh/.cache/rubocop_cache/da7c0adf3cdca36c6db455c7f909e01abe58503a/0536d18c9eb040c724be8df71fd74a67730b58ef/027b8e17147ee3c7f4fce30a50ffb44ff511b6cb
C

Offenses:

hash.rb:4:10: C: [Corrected] Style/HashSyntax: Omit the hash value.
{ var => var }
         ^^^

1 file inspected, 1 offense detected, 1 offense corrected
Finished in 0.18334346099800314 seconds

The code after autocorrection:

# frozen_string_literal: true

var = 1
{ var: } # resulting in { var: 1}

Steps to reproduce the problem

See above

RuboCop version

❯ rubocop -V
1.25.1 (using Parser 3.1.0.0, rubocop-ast 1.15.1, running on ruby 3.1.0 x86_64-linux)
@koic
Copy link
Member

koic commented Feb 5, 2022

This is not a false positive because it is an expected behavior of EnforcedShorthandSyntax: always with Ruby 3.1. If you would not like to omit the hash value, please set EnforcedShorthandSyntax: never or EnforcedShorthandSyntax: either.
https://docs.rubocop.org/rubocop/1.25/cops_style.html#enforcedshorthandsyntax-always-default

@koic koic closed this as completed Feb 5, 2022
@mehanoid
Copy link
Author

mehanoid commented Feb 5, 2022

@koic hash value omission is only possible when key is a Symbol, otherwise it just breaks the code like in the example above.

@mehanoid
Copy link
Author

mehanoid commented Feb 5, 2022

I've updated the examples with comments reflecting the result of running the code

@koic
Copy link
Member

koic commented Feb 5, 2022

Ah, you are right. It's my oversight.

@koic koic reopened this Feb 5, 2022
@koic koic added the bug label Feb 5, 2022
koic added a commit to koic/rubocop that referenced this issue Feb 5, 2022
Fixes rubocop#10401.

This PR fixes a false positive for `Style/HashSyntax`
when lvar hash key and hash value are the same
koic added a commit to koic/rubocop that referenced this issue Feb 5, 2022
Fixes rubocop#10401.

This PR fixes a false positive for `Style/HashSyntax`
when local variable hash key and hash value are the same.
koic added a commit to koic/rubocop that referenced this issue Feb 5, 2022
Fixes rubocop#10401.

This PR fixes a false positive for `Style/HashSyntax`
when local variable hash key and hash value are the same.
koic added a commit that referenced this issue Feb 5, 2022
…sh_syntax

[Fix #10401] Fix a false positive for `Style/HashSyntax`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants