Class: RuboCop::Cop::Lint::UnreachableLoop

Inherits:
Base
  • Object
show all
Includes:
IgnoredPattern
Defined in:
lib/rubocop/cop/lint/unreachable_loop.rb

Overview

This cop checks for loops that will have at most one iteration.

A loop that can never reach the second iteration is a possible error in the code. In rare cases where only one iteration (or at most one iteration) is intended behavior, the code should be refactored to use `if` conditionals.

NOTE: Block methods that are used with `Enumerable`s are considered to be loops.

`IgnoredPatterns` can be used to match against the block receiver in order to allow code that would otherwise be registered as an offense (eg. `times` used not in an `Enumerable` context).

Examples:

# bad
while node
  do_something(node)
  node = node.parent
  break
end

# good
while node
  do_something(node)
  node = node.parent
end

# bad
def verify_list(head)
  item = head
  begin
    if verify(item)
      return true
    else
      return false
    end
  end while(item)
end

# good
def verify_list(head)
  item = head
  begin
    if verify(item)
      item = item.next
    else
      return false
    end
  end while(item)

  true
end

# bad
def find_something(items)
  items.each do |item|
    if something?(item)
      return item
    else
      raise NotFoundError
    end
  end
end

# good
def find_something(items)
  items.each do |item|
    if something?(item)
      return item
    end
  end
  raise NotFoundError
end

# bad
2.times { raise ArgumentError }

IgnoredPatterns: [/(exactly|at_least|at_most)(d+).times/] (default)


# good
exactly(2).times { raise StandardError }

Constant Summary collapse

MSG =
'This loop will have at most one iteration.'
CONTINUE_KEYWORDS =
%i[next redo].freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_block(node) ⇒ Object


100
101
102
# File 'lib/rubocop/cop/lint/unreachable_loop.rb', line 100

def on_block(node)
  check(node) if loop_method?(node)
end

#on_while(node) ⇒ Object Also known as: on_until, on_while_post, on_until_post, on_for


92
93
94
# File 'lib/rubocop/cop/lint/unreachable_loop.rb', line 92

def on_while(node)
  check(node)
end