class RuboCop::Cop::Lint::UnreachableCode
Checks for unreachable code. The check are based on the presence of flow of control statement in non-final position in ‘begin` (implicit) blocks.
@example
# bad def some_method return do_something end # bad def some_method if cond return else return end do_something end
@example
# good def some_method do_something end
Constants
- MSG
Public Instance Methods
on_begin(node)
click to toggle source
# File lib/rubocop/cop/lint/unreachable_code.rb, line 40 def on_begin(node) expressions = *node expressions.each_cons(2) do |expression1, expression2| next unless flow_expression?(expression1) add_offense(expression2) end end
Also aliased as: on_kwbegin
Private Instance Methods
check_case(node)
click to toggle source
# File lib/rubocop/cop/lint/unreachable_code.rb, line 87 def check_case(node) else_branch = node.else_branch return false unless else_branch return false unless flow_expression?(else_branch) node.when_branches.all? { |branch| branch.body && flow_expression?(branch.body) } end
check_if(node)
click to toggle source
# File lib/rubocop/cop/lint/unreachable_code.rb, line 81 def check_if(node) if_branch = node.if_branch else_branch = node.else_branch if_branch && else_branch && flow_expression?(if_branch) && flow_expression?(else_branch) end
flow_expression?(node)
click to toggle source
# File lib/rubocop/cop/lint/unreachable_code.rb, line 65 def flow_expression?(node) return true if flow_command?(node) case node.type when :begin, :kwbegin expressions = *node expressions.any? { |expr| flow_expression?(expr) } when :if check_if(node) when :case check_case(node) else false end end