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

False positive with Style/RedundantEach cop #11165

Closed
anthony-robin opened this issue Nov 9, 2022 · 0 comments · Fixed by #11166
Closed

False positive with Style/RedundantEach cop #11165

anthony-robin opened this issue Nov 9, 2022 · 0 comments · Fixed by #11166
Labels

Comments

@anthony-robin
Copy link
Contributor

Given the folliwing Ruby code

string.each_char
      .map(&:to_i)
      .reverse
      .each_with_index do |item, index|
  puts item, index
end

Rubocop throws an offense

tmp/test_rubocop.rb:4:8: C: [Correctable] Style/RedundantEach: Use with_index to remove redundant each.
      .each_with_index do |item, index|
       ^^^^^^^^^^^^^^^

and autocorrects it to

string.each_char
      .map(&:to_i)
      .reverse
      .with_index do |item, index|
  puts item, index
end

which is a false positive according to the following error:

 NoMethodError:
        undefined method `with_index' for [1, 3, 1, 0, 0, 8, 8, 9, 6, 9, 5, 6, 0, 7]:Array

                  .with_index do |digit, idx|
                  ^^^^^^^^^^^
        Did you mean?  with_options

Thank you :)

RuboCop version

$ [bundle exec] rubocop -V
1.38.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [x86_64-darwin21]
  - rubocop-performance 1.15.0
  - rubocop-rails 2.17.2
  - rubocop-rspec 2.15.0
@koic koic added the bug label Nov 9, 2022
koic added a commit to koic/rubocop that referenced this issue Nov 9, 2022
Follow up faker-ruby/faker#2613 (comment).
Fixes rubocop#11165.

This PR fixes a false positive for `Style/RedundantEach`
when any method is used between methods containing `each` in the method name.

That method in between may convert from `Enumerator` to `Array`. e.g. `map(&:do_something)`

```ruby
'string'.each_char.class             # => Enumerator
'string'.each_char.map(&:to_i).class # => Array
```

`each_with_index` is a method of `Array`, not of `Enumerator`.

```ruby
'string'.each_char.map(&:to_i).respond_to?(:each_with_index) # => true
'string'.each_char.map(&:to_i).respond_to?(:with_index)      # => false
```

Therefore, it allows any method to be used between methods containing `each` to
prevent `NoMethodError`.
bbatsov pushed a commit that referenced this issue Nov 10, 2022
Follow up faker-ruby/faker#2613 (comment).
Fixes #11165.

This PR fixes a false positive for `Style/RedundantEach`
when any method is used between methods containing `each` in the method name.

That method in between may convert from `Enumerator` to `Array`. e.g. `map(&:do_something)`

```ruby
'string'.each_char.class             # => Enumerator
'string'.each_char.map(&:to_i).class # => Array
```

`each_with_index` is a method of `Array`, not of `Enumerator`.

```ruby
'string'.each_char.map(&:to_i).respond_to?(:each_with_index) # => true
'string'.each_char.map(&:to_i).respond_to?(:with_index)      # => false
```

Therefore, it allows any method to be used between methods containing `each` to
prevent `NoMethodError`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants