You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
We have an idiom of enum_for/to_enum method for defining an iterator.
classCdefeach_item(&block)returnenum_for(__method__)unlessblock_given?iterate_someting_with_blockblockendendc=C.new# We can call C#each with and without a block.c.each_itemdo |item|
endc.each_item.mapdo |item|
end
It is a useful idiom! But we sometimes make a bug if the iterator receives arguments. For example:
classCdefeach_item(type, &block)returnenum_for(__method__)unlessblock_given?iterate_something_with_blocktype,blockendendc=C.new# It works well.c.each_item(:foo)do |item|
end# But it does not work well because enum_for should receive `type` but actually doesn't.c.each_item(:foo).mapdo |item|
end
It should be return enum_for(__method__, type) unless block_given?.
In this case, it is not a serious problem because c.each_item(:foo).map{} raises an ArgumentError.
But it will be serious when the argument is optional, like def each_item(type = :default, &block).
It ignores type argument silently.
Describe the solution you'd like
Add a new cop to detect this problem.
Describe alternatives you've considered
Nothing
Additional context
to_enum is an alias of enum_for
Probably we should focus the idiom, it means the cop should accept any enum_for without return enum_for(__method__) unless block_given? form.
The text was updated successfully, but these errors were encountered:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!
Cop should only apply when: the receiver is nil or self.
Maybe it should check that either the method name (in your example :each_item) or __method__ is used as the first argument too.
Is your feature request related to a problem? Please describe.
We have an idiom of
enum_for
/to_enum
method for defining an iterator.It is a useful idiom! But we sometimes make a bug if the iterator receives arguments. For example:
It should be
return enum_for(__method__, type) unless block_given?
.In this case, it is not a serious problem because
c.each_item(:foo).map{}
raises anArgumentError
.But it will be serious when the argument is optional, like
def each_item(type = :default, &block)
.It ignores
type
argument silently.Describe the solution you'd like
Add a new cop to detect this problem.
Describe alternatives you've considered
Nothing
Additional context
enum_for
withoutreturn enum_for(__method__) unless block_given?
form.The text was updated successfully, but these errors were encountered: