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

Rails/WithIndex False positive #350

Closed
Tietew opened this issue Sep 8, 2020 · 0 comments
Closed

Rails/WithIndex False positive #350

Tietew opened this issue Sep 8, 2020 · 0 comments

Comments

@Tietew
Copy link
Contributor

Tietew commented Sep 8, 2020

When each_with_object is used with multiplex assignment:

def make_field_name_map(model)
  model.attribute_names.each_with_object({}) { |name, hash|
    hash[name] = hash[name.humanize] = hash[model.human_attribute_name(name)] = name.to_sym
  }
end

Expected behavior

No offences.

Actual behavior

Rubocop detects Rails/WithIndex offence:

test.rb:2:3: C: Rails/IndexWith: Prefer index_with over each_with_object.
  model.attribute_names.each_with_object({}) { |name, hash| ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Autocorrect breaks code:

def make_field_name_map(model)
  model.attribute_names.index_with { |name|
    hash[name.humanize] = hash[model.human_attribute_name(name)] = name.to_sym
  }
end

RuboCop version

$ be rubocop -V
0.90.0 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.6.6 x86_64-linux)
$ grep rails Gemfile.lock
    rubocop-rails (2.8.0)
@koic koic closed this as completed in d8c62f5 Sep 10, 2020
koic added a commit that referenced this issue Sep 10, 2020
[Fix #338][Fix #350] Fix a false positive for Rails/IndexBy and Rails/IndexWith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant