Class: RuboCop::Cop::Lint::UnreachableLoop
- 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).
Constant Summary collapse
- MSG =
'This loop will have at most one iteration.'
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #on_block(node) ⇒ Object
- #on_while(node) ⇒ Object (also: #on_until, #on_while_post, #on_until_post, #on_for)
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, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#on_block(node) ⇒ Object
99 100 101 |
# File 'lib/rubocop/cop/lint/unreachable_loop.rb', line 99 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
91 92 93 |
# File 'lib/rubocop/cop/lint/unreachable_loop.rb', line 91 def on_while(node) check(node) end |