Skip to content

Error in Rails/IndexWith when using nil #1463

Open
@ydakuka

Description

@ydakuka

Describe

I have code:

arr.to_h { |id| [id, 0] }

Then I run rubocop with autocorrect and get:

arr.index_with { |_id| 0 }

However:

arr = [1, 2, 3]
arr.to_h { |id| [id, 0] }  # => {1=>0, 2=>0, 3=>0}
arr.index_with { |_id| 0 } # => {1=>0, 2=>0, 3=>0}

arr = nil
arr.to_h { |id| [id, 0] }  # => {}
arr.index_with { |_id| 0 } # NoMethodError: undefined method `index_with' for nil:NilClass

RuboCop

$ rubocop -V
1.72.2 (using Parser 3.3.7.1, rubocop-ast 1.38.0, analyzing as Ruby 2.7, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-performance 1.23.1
  - rubocop-rails 2.29.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 3.4.0
  - rubocop-rspec_rails 2.30.0
  - rubocop-thread_safety 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