Class: RuboCop::Cop::Lint::UnreachableLoop
- 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).
Constant Summary collapse
- MSG =
'This loop will have at most one iteration.'
- CONTINUE_KEYWORDS =
%i[next redo].freeze
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_block(node) ⇒ Object
- #on_numblock(node) ⇒ Object
- #on_while(node) ⇒ Object (also: #on_until, #on_while_post, #on_until_post, #on_for)
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, 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, #inspect, 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
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
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 |