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

Style/MapIntoArray misinterprets my class design #12894

Closed
yegor256 opened this issue May 10, 2024 · 3 comments · Fixed by #12896
Closed

Style/MapIntoArray misinterprets my class design #12894

yegor256 opened this issue May 10, 2024 · 3 comments · Fixed by #12896

Comments

@yegor256
Copy link

This is the code:

class Foo
  def each
    (0..42).each do |i|
      yield i
    end
  end

  def to_a
    array = []
    each { |i| array << i }
    array
  end
end

This is my config:

AllCops:
  TargetRubyVersion: 3.2
  NewCops: enable

Rubocop 1.63.5 complains (which is a wrong complaint):

$ rubocop --config my.cfg a.rb
a.rb:10:5: C: [Correctable] Style/MapIntoArray: Use map instead of each to map elements into an array.
    each { |i| array << i }
    ^^^^^^^^^^^^^^^^^^^^^^^

Then, I run rubocop -A and it produced a broken code:

class Foo
  def each(&)
    (0..42).each(&)
  end

  def to_a
    map { |i| i }  # error here, there is no "map" method in the class
  end
end
@yegor256 yegor256 changed the title Style/MapIntoArray misinterpret my class design Style/MapIntoArray misinterprets my class design May 10, 2024
@koic
Copy link
Member

koic commented May 10, 2024

Style/MapIntoArray is already marked as unsafe. If you expect safe autocorrect, please use -a instead of -A. Thank you.
https://docs.rubocop.org/rubocop/1.63/cops_style.html#stylemapintoarray

@koic koic closed this as completed May 10, 2024
@yegor256
Copy link
Author

yegor256 commented May 10, 2024

@koic auto-correction is not the issue here. The main problem is the wrong complaint. I believe, it should be fixed. Currently, I have to suppress, look: https://github.com/yegor256/factbase/blob/master/lib/factbase/query.rb#L49-L55

@koic koic reopened this May 11, 2024
koic added a commit to koic/rubocop that referenced this issue May 11, 2024
Fixes rubocop#12894.

This PR fixes false positives for `Style/MapIntoArray`
when using `each` without receiver with `<<` to build an array.
@koic
Copy link
Member

koic commented May 11, 2024

Ah, sure. In most contexts that this Cop should detect, the receiver is usually not omitted. This means that cases where the receiver is omitted can be considered as not Enumerable#each and can be allowed. I think the potential for false negatives with this change is lower than the potential for false positives. I opened PR #12896 to resolve this issue. Thank you!

koic added a commit that referenced this issue May 13, 2024
…_into_array

[Fix #12894] Fix false positives for `Style/MapIntoArray`
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

Successfully merging a pull request may close this issue.

2 participants