`Style/MapIntoArray` misinterprets my class design
yegor256 opened this issue · comments
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
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 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
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!