class RuboCop::Cop::Lint::UnreachableLoop
exactly(2).times { raise StandardError }
# good
@example AllowedPatterns: [‘(exactly|at_least|at_most)(d+).times’] (default)
2.times { raise ArgumentError }
# bad
end
raise NotFoundError
end
end
return item
if something?(item)
items.each do |item|
def find_something(items)
# good
end
end
end
raise NotFoundError
else
return item
if something?(item)
items.each do |item|
def find_something(items)
# bad
end
true
end while(item)
end
return false
else
item = item.next
if verify(item)
begin
item = head
def verify_list(head)
# good
end
end while(item)
end
return false
else
return true
if verify(item)
begin
item = head
def verify_list(head)
# bad
end
node = node.parent
do_something(node)
while node
# good
end
break
node = node.parent
do_something(node)
while node
# bad
@example
`Enumerable` context).
code that would otherwise be registered as an offense (eg. ‘times` used not in an
`AllowedPatterns` can be used to match against the block receiver in order to allow
NOTE: Block methods that are used with “Enumerable“s are considered to be loops.
the code should be refactored to use `if` conditionals.
In rare cases where only one iteration (or at most one iteration) is intended behavior,
A loop that can never reach the second iteration is a possible error in the code.
Checks for loops that will have at most one iteration.
def break_statement?(node)
def break_statement?(node) return true if break_command?(node) case node.type when :begin, :kwbegin statements = *node break_statement = statements.find { |statement| break_statement?(statement) } break_statement && !preceded_by_continue_statement?(break_statement) when :if check_if(node) when :case check_case(node) else false end end
def check(node)
def check(node) statements = statements(node) break_statement = statements.find { |statement| break_statement?(statement) } return unless break_statement unless preceded_by_continue_statement?(break_statement) || conditional_continue_keyword?(break_statement) add_offense(node) end end
def check_case(node)
def check_case(node) else_branch = node.else_branch return false unless else_branch return false unless break_statement?(else_branch) node.when_branches.all? { |branch| branch.body && break_statement?(branch.body) } end
def check_if(node)
def check_if(node) if_branch = node.if_branch else_branch = node.else_branch if_branch && else_branch && break_statement?(if_branch) && break_statement?(else_branch) end
def conditional_continue_keyword?(break_statement)
def conditional_continue_keyword?(break_statement) or_node = break_statement.each_descendant(:or).to_a.last or_node && CONTINUE_KEYWORDS.include?(or_node.rhs.type) end
def loop_method?(node)
def loop_method?(node) return false unless node.block_type? || node.numblock_type? send_node = node.send_node return false if matches_allowed_pattern?(send_node.source) send_node.enumerable_method? || send_node.enumerator_method? || send_node.method?(:loop) end
def on_block(node)
def on_block(node) check(node) if loop_method?(node) end
def on_numblock(node)
def on_numblock(node) check(node) if loop_method?(node) end
def on_while(node)
def on_while(node) check(node) end
def preceded_by_continue_statement?(break_statement)
def preceded_by_continue_statement?(break_statement) break_statement.left_siblings.any? do |sibling| # Numblocks have the arguments count as a number in the AST. next if sibling.is_a?(Integer) next if sibling.loop_keyword? || loop_method?(sibling) sibling.each_descendant(*CONTINUE_KEYWORDS).any? end end
def statements(node)
def statements(node) body = node.body if body.nil? [] elsif body.begin_type? body.children else [body] end end