Class: RuboCop::Cop::Obsession::NoBreakOrNext
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Obsession::NoBreakOrNext
- Defined in:
- lib/rubocop/cop/obsession/no_break_or_next.rb
Overview
This cop checks for next (and sometimes break) in loops.
- For big loops,
nextorbreakindicates that the loop body has significant logic, which means it should be moved into its own method, and you can convert thenextorbreakintoreturnand the like. - For small loops, you can just use normal conditions instead of
next.breakis allowed.
Note: Sometimes loops can also be rethought, like transforming a
loop + break into a while.
Constant Summary collapse
- BIG_LOOP_MSG =
'Avoid `break`/`next` in big loop, decompose into private method or rethink loop.'- NO_NEXT_MSG =
'Avoid `next` in loop, use conditions or rethink loop.'- BIG_LOOP_MIN_LINES =
7
Instance Method Summary collapse
Instance Method Details
#on_block(node) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/rubocop/cop/obsession/no_break_or_next.rb', line 75 def on_block(node) return if !contains_break_or_next?(node) if big_loop?(node) add_offense(node, message: BIG_LOOP_MSG) elsif contains_next?(node) add_offense(node, message: NO_NEXT_MSG) end end |