Skip to content

Commit

Permalink
Merge pull request rubocop#9318 from agargiulo/style/empty_literal_ha…
Browse files Browse the repository at this point in the history
…sh_fix

[Fix rubocop#9316] Make `Style/EmptyLiteral` not register offense when using a numbered block
  • Loading branch information
koic committed Jan 3, 2021
2 parents 2664ece + d919058 commit a408b5b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5316,3 +5316,4 @@
[@sswander]: https://github.com/sswander
[@makicamel]: https://github.com/makicamel
[@h-lame]: https://github.com/h-lame
[@agargiulo]: https://github.com/agargiulo
1 change: 1 addition & 0 deletions changelog/fix_incorrect_offense_for_style_empty_literal.md
@@ -0,0 +1 @@
* [#9316](https://github.com/rubocop-hq/rubocop/issues/9316): Fix `Style/EmptyLiteral` registering wrong offense when using a numbered block for Hash.new, i.e. `Hash.new { _1[_2] = [] }`. ([@agargiulo][])
8 changes: 6 additions & 2 deletions lib/rubocop/cop/style/empty_literal.rb
Expand Up @@ -32,8 +32,12 @@ class EmptyLiteral < Base
def_node_matcher :str_node, '(send (const {nil? cbase} :String) :new)'
def_node_matcher :array_with_block,
'(block (send (const {nil? cbase} :Array) :new) args _)'
def_node_matcher :hash_with_block,
'(block (send (const {nil? cbase} :Hash) :new) args _)'
def_node_matcher :hash_with_block, <<~PATTERN
{
(block (send (const {nil? cbase} :Hash) :new) args _)
(numblock (send (const {nil? cbase} :Hash) :new) ...)
}
PATTERN

def on_send(node)
return unless (message = offense_message(node))
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/style/empty_literal_spec.rb
Expand Up @@ -108,6 +108,16 @@
expect_no_offenses('test = ::Hash.new { block }')
end

context 'Ruby 2.7', :ruby27 do
it 'does not register an offense for Hash.new { _1[_2] = [] }' do
expect_no_offenses('test = Hash.new { _1[_2] = [] }')
end

it 'does not register an offense for ::Hash.new { _1[_2] = [] }' do
expect_no_offenses('test = ::Hash.new { _1[_2] = [] }')
end
end

it 'auto-corrects Hash.new in block ' do
expect_offense(<<~RUBY)
puts { Hash.new }
Expand Down

0 comments on commit a408b5b

Please sign in to comment.