class RuboCop::Cop::Lint::NonLocalExitFromIterator
end
end
end
end
item.update!(foobar: true)
return if item.stock == 0 # false-negative…
find_each do |item| # block without method chain
return unless update_necessary? # allowed
transaction do # block without arguments
def update_items
end
{ message: ‘validation error’, errors: error_array }
end
“#{error.param}: #{error.message}”
return “#{error.param}: invalid” unless error.message # allowed
return if error.suppress? # warned
error_array = e.errors.map do |error| # block with method chain
return { message: ‘validation error’ } unless e.errors # allowed
rescue_from ValidationError do |e| # non-iteration block with arg
class ItemApi
@example
method definition.
* the return is not contained in an inner scope, e.g. a lambda or a
or ‘define_singleton_method`,
* the method which receives the block is not `define_method`
* the block has arguments,
* the block is preceded by a method chain,
* No value is returned,
value. It registers an offense under these conditions:
Checks for non-local exits from iterators without a return
def on_return(return_node)
def on_return(return_node) return if return_value?(return_node) return_node.each_ancestor(:block, :def, :defs) do |node| break if scoped_node?(node) # if a proc is passed to `Module#define_method` or # `Object#define_singleton_method`, `return` will not cause a # non-local exit error break if define_method?(node.send_node) next unless node.arguments? if chained_send?(node.send_node) add_offense(return_node.loc.keyword) break end end end
def return_value?(return_node)
def return_value?(return_node) !return_node.children.empty? end
def scoped_node?(node)
def scoped_node?(node) node.def_type? || node.defs_type? || node.lambda? end