Skip to content

Commit

Permalink
Fix an error for Style/HashAsLastArrayItem with no_braces for empty h…
Browse files Browse the repository at this point in the history
…ash (#8681)

When `EnforcedStyle` is set to `no_braces`, rubocop was attempting to
correct `[1, {}]` into `[1, ]`, which is not valid. An empty hash cannot
omit the braces.

Co-authored-by: Bozhidar Batsov <bozhidar@batsov.com>
  • Loading branch information
fsateler and bbatsov committed Sep 15, 2020
1 parent 295b9a6 commit 80e5740
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@
* [#8664](https://github.com/rubocop-hq/rubocop/issues/8664): Fix a false positive for `Naming/BinaryOperatorParameterName` when naming multibyte character method name. ([@koic][])
* [#8604](https://github.com/rubocop-hq/rubocop/issues/8604): Fix a false positive for `Bundler/DuplicatedGem` when gem is duplciated in condition. ([@tejasbubane][])
* [#8671](https://github.com/rubocop-hq/rubocop/issues/8671): Fix an error for `Style/ExplicitBlockArgument` when using safe navigation method call. ([@koic][])
* [#8681](https://github.com/rubocop-hq/rubocop/pull/8681): Fix an error for `Style/HashAsLastArrayItem` with `no_braces` for empty hash. ([@fsateler][])
* [#8682](https://github.com/rubocop-hq/rubocop/pull/8682): Fix a positive for `Style/HashTransformKeys` and `Style/HashTransformValues` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][])
* [#8688](https://github.com/rubocop-hq/rubocop/issues/8688): Mark `Style/GlobalStdStream` as unsafe autocorrection. ([@marcandre][])
* [#8642](https://github.com/rubocop-hq/rubocop/issues/8642): Fix a false negative for `Style/SpaceInsideHashLiteralBraces` when a correct empty hash precedes the incorrect hash. ([@dvandersluis][])
Expand Down Expand Up @@ -4871,4 +4872,5 @@
[@jaimerave]: https://github.com/jaimerave
[@Skipants]: https://github.com/Skipants
[@sascha-wolf]: https://github.com/sascha-wolf
[@iSarCasm]: https://github.com/iSarCasm
[@fsateler]: https://github.com/fsateler
[@iSarCasm]: https://github.com/iSarCasm
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/hash_as_last_array_item.rb
Expand Up @@ -62,6 +62,7 @@ def check_braces(node)

def check_no_braces(node)
return unless node.braces?
return if node.children.empty? # Empty hash cannot be "unbraced"

add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
corrector.remove(node.loc.begin)
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/hash_as_last_array_item_spec.rb
Expand Up @@ -36,6 +36,12 @@
[{ one: 1 }, { two: 2 }]
RUBY
end

it 'does not register an offense when the hash is empty' do
expect_no_offenses(<<~RUBY)
[1, {}]
RUBY
end
end

context 'when EnforcedStyle is no_braces' do
Expand Down Expand Up @@ -71,5 +77,11 @@
[{ one: 1 }, { two: 2 }]
RUBY
end

it 'does not register an offense when the hash is empty' do
expect_no_offenses(<<~RUBY)
[1, {}]
RUBY
end
end
end

0 comments on commit 80e5740

Please sign in to comment.