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

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

Overview

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.

‘AllowedPatterns` 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 }

AllowedPatterns: [‘(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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, 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?, #autocorrect_with_disable_uncorrectable?, #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_numblock(node) ⇒ Object



104
105
106
# File 'lib/rubocop/cop/lint/unreachable_loop.rb', line 104

def on_numblock(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