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/IndexWith false positive #338

Closed
rasaffie opened this issue Aug 27, 2020 · 2 comments
Closed

Rails/IndexWith false positive #338

rasaffie opened this issue Aug 27, 2020 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rasaffie
Copy link

Expected behavior

Don't auto-correct inject on unsafe cases of Style/EachWithObject + Rails/IndexWith.

Actual behavior

It replaces inject with index_with, but with an undefined variable.

Steps to reproduce the problem

  • With code:
def self.make_hash(names)
  names.inject({}) { |result, name| result[name] = Date::MONTHNAMES[result.count + 1]; result }
end
  • Run rubocop -a
  • It returns:
def self.make_hash(names)
  names.index_with { |_name| Date::MONTHNAMES[result.count + 1]; }
end

where result.count raises exception.

RuboCop version

$ [bundle exec] rubocop -V
0.89.1 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.7.1 x86_64-linux)
@marcandre
Copy link

Indeed, any other reference to the block variable to be eliminated should imply no offense at all.

@marcandre marcandre transferred this issue from rubocop/rubocop Aug 27, 2020
@marcandre
Copy link

Transferring to RuboCop/Rails as Style/EachWithObject's auto-correction is fine:

def self.make_hash(names)
  names.each_with_object({}) { |name, result| result[name] = Date::MONTHNAMES[result.count + 1]; }
end

It's the subsequent transformation with index_by that isn't.

@marcandre marcandre changed the title Unsafe auto-correct for inject Rails/IndexWith false positive Aug 27, 2020
@marcandre marcandre added bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed labels Aug 27, 2020
@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
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants