Skip to content

Style/EmptyLiteral erroneously thinks Hash.new([]) is equal to {} #13181

Closed
@markijbema

Description

@markijbema

Expected behavior

I would have expected RuboCop not to consider Hash.new([]) as an empty literal, as it is an empty hash with a default value ([]), whereas {} has no default value

Actual behavior

Rubocop detects a problem and suggests a fix which breaks the code (because the default value is missing:

Offenses:

foo.rb:42:16: C: [Correctable] Style/EmptyLiteral: Use hash literal {} instead of Hash.new([]).
      result = Hash.new([])

Steps to reproduce the problem

Add Hash.new([]) somewhere in code which is checked by Rubocop

RuboCop version

$ bundle exec rubocop -V     
1.66.0 (using Parser 3.3.4.2, rubocop-ast 1.32.1, running on ruby 3.2.0) [aarch64-linux]
  - rubocop-factory_bot 2.26.1
  - rubocop-graphql 1.5.4
  - rubocop-minitest 0.36.0
  - rubocop-performance 1.21.1
  - rubocop-rails 2.26.0
  - rubocop-rake 0.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions